Skip to content

Commit bd8ef68

Browse files
committed
Extract V4L2 video helpers to reusable library. Move generic GStreamer V4L2 video helper functions from Video_Encode_Decode test to shared lib_gstreamer.sh for code reuse across all GStreamer tests
Signed-off-by: Nitin Nakka <nitinn@qti.qualcomm.com>
1 parent 93f6153 commit bd8ef68

3 files changed

Lines changed: 407 additions & 165 deletions

File tree

Runner/suites/Multimedia/GSTreamer/Video/Video_Encode_Decode/README.md

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Runner/suites/Multimedia/GSTreamer/Video/Video_Encode_Decode/run.sh
2626

2727
Required shared utils (sourced from `Runner/utils` via `init_env`):
2828
- `functestlib.sh`
29-
- `lib_gstreamer.sh`
29+
- `lib_gstreamer.sh` - **Contains reusable V4L2 video helpers** (see Library Functions section below)
3030
- optional: `lib_video.sh` (for video stack management)
3131

3232
---
@@ -417,6 +417,153 @@ Where:
417417

418418
---
419419

420+
## Library Functions (Runner/utils/lib_gstreamer.sh)
421+
422+
This test uses reusable helper functions from `lib_gstreamer.sh` that other GStreamer tests can leverage:
423+
424+
### Resolution and Codec Helpers
425+
426+
**`gstreamer_resolution_to_wh <resolution>`**
427+
- Converts resolution names to width/height
428+
- Input: `480p`, `720p`, `1080p`, `4k`
429+
- Output: `"<width> <height>"` (e.g., `"1920 1080"`)
430+
- Example:
431+
```sh
432+
params=$(gstreamer_resolution_to_wh "1080p")
433+
width=$(printf '%s' "$params" | awk '{print $1}') # 1920
434+
height=$(printf '%s' "$params" | awk '{print $2}') # 1080
435+
```
436+
437+
**`gstreamer_v4l2_encoder_for_codec <codec>`**
438+
- Returns V4L2 encoder element for codec
439+
- Input: `h264`, `h265`/`hevc`
440+
- Output: `v4l2h264enc` or `v4l2h265enc` (or empty if not available)
441+
- Example:
442+
```sh
443+
encoder=$(gstreamer_v4l2_encoder_for_codec "h264") # v4l2h264enc
444+
```
445+
446+
**`gstreamer_v4l2_decoder_for_codec <codec>`**
447+
- Returns V4L2 decoder element for codec
448+
- Input: `h264`, `h265`/`hevc`, `vp9`
449+
- Output: `v4l2h264dec`, `v4l2h265dec`, or `v4l2vp9dec` (or empty if not available)
450+
- Example:
451+
```sh
452+
decoder=$(gstreamer_v4l2_decoder_for_codec "vp9") # v4l2vp9dec
453+
```
454+
455+
**`gstreamer_container_ext_for_codec <codec>`**
456+
- Returns file extension for codec
457+
- Input: `h264`, `h265`, `vp9`
458+
- Output: `mp4` (for h264/h265) or `ivf` (for vp9)
459+
- Example:
460+
```sh
461+
ext=$(gstreamer_container_ext_for_codec "h264") # mp4
462+
```
463+
464+
### Bitrate and File Size Helpers
465+
466+
**`gstreamer_bitrate_for_resolution <width> <height>`**
467+
- Calculates recommended bitrate based on resolution
468+
- Returns bitrate in bps
469+
- Bitrate mapping:
470+
- ≤640px width: 1 Mbps (1000000 bps)
471+
- ≤1280px width: 2 Mbps (2000000 bps)
472+
- ≤1920px width: 4 Mbps (4000000 bps)
473+
- >1920px width: 8 Mbps (8000000 bps)
474+
- Example:
475+
```sh
476+
bitrate=$(gstreamer_bitrate_for_resolution 1920 1080) # 4000000
477+
```
478+
479+
**`gstreamer_file_size_bytes <filepath>`**
480+
- Returns file size in bytes (portable across BSD/GNU stat)
481+
- Returns `0` if file doesn't exist
482+
- Example:
483+
```sh
484+
size=$(gstreamer_file_size_bytes "/tmp/video.mp4")
485+
if [ "$size" -gt 1000 ]; then
486+
echo "File is valid"
487+
fi
488+
```
489+
490+
### Pipeline Builders
491+
492+
**`gstreamer_build_v4l2_encode_pipeline <codec> <width> <height> <duration> <framerate> <bitrate> <output_file> <video_stack>`**
493+
- Builds complete V4L2 encode pipeline with videotestsrc
494+
- Parameters:
495+
- `codec`: `h264` or `h265`
496+
- `width`, `height`: Video dimensions
497+
- `duration`: Duration in seconds
498+
- `framerate`: Frames per second
499+
- `bitrate`: Bitrate in bps
500+
- `output_file`: Output file path
501+
- `video_stack`: `upstream` or `downstream` (adds IO mode parameters for downstream)
502+
- Returns: Complete pipeline string (or empty if encoder not available)
503+
- Example:
504+
```sh
505+
pipeline=$(gstreamer_build_v4l2_encode_pipeline \
506+
"h264" 1920 1080 30 30 4000000 "/tmp/test.mp4" "upstream")
507+
gstreamer_run_gstlaunch_timeout 40 "$pipeline"
508+
```
509+
510+
**`gstreamer_build_v4l2_decode_pipeline <codec> <input_file> <video_stack>`**
511+
- Builds complete V4L2 decode pipeline
512+
- Parameters:
513+
- `codec`: `h264`, `h265`, or `vp9`
514+
- `input_file`: Input file path
515+
- `video_stack`: `upstream` or `downstream`
516+
- Returns: Complete pipeline string (or empty if decoder not available)
517+
- Automatically handles:
518+
- Container format (MP4 for h264/h265, IVF for vp9)
519+
- Parser selection (h264parse, h265parse, ivfparse)
520+
- IO mode parameters for downstream stack
521+
- Example:
522+
```sh
523+
pipeline=$(gstreamer_build_v4l2_decode_pipeline \
524+
"h264" "/tmp/test.mp4" "upstream")
525+
gstreamer_run_gstlaunch_timeout 40 "$pipeline"
526+
```
527+
528+
### Usage in Other Tests
529+
530+
To use these functions in your GStreamer test:
531+
532+
```sh
533+
#!/bin/sh
534+
# Source init_env and lib_gstreamer.sh
535+
. "$INIT_ENV"
536+
. "$TOOLS/functestlib.sh"
537+
. "$TOOLS/lib_gstreamer.sh"
538+
539+
# Use the helpers
540+
params=$(gstreamer_resolution_to_wh "4k")
541+
width=$(printf '%s' "$params" | awk '{print $1}')
542+
height=$(printf '%s' "$params" | awk '{print $2}')
543+
544+
bitrate=$(gstreamer_bitrate_for_resolution "$width" "$height")
545+
546+
pipeline=$(gstreamer_build_v4l2_encode_pipeline \
547+
"h264" "$width" "$height" 10 30 "$bitrate" "/tmp/output.mp4" "upstream")
548+
549+
if [ -n "$pipeline" ]; then
550+
gstreamer_run_gstlaunch_timeout 20 "$pipeline"
551+
fi
552+
```
553+
554+
### Testing Pipeline Builders
555+
556+
A test script is provided to verify the pipeline builders:
557+
558+
```bash
559+
cd Runner/suites/Multimedia/GSTreamer/Video/Video_Encode_Decode
560+
sh test_pipeline_builders.sh
561+
```
562+
563+
This will output example pipelines for various codecs, resolutions, and video stacks.
564+
565+
---
566+
420567
## Notes for CI / LAVA
421568

422569
- The test always exits `0`.

0 commit comments

Comments
 (0)