[FFmpeg-devel] mpeg12enc: Use Closed Captions if available

Submitted by Mathieu Duponchelle on Feb. 7, 2019, 6:11 p.m.

Details

Message ID 20190207181116.29795-1-mathieu@centricular.com
State New
Headers show

Commit Message

Mathieu Duponchelle Feb. 7, 2019, 6:11 p.m.
---
 doc/encoders.texi      |  3 +++
 libavcodec/mpeg12enc.c | 29 +++++++++++++++++++++++++++++
 libavcodec/mpegvideo.h |  2 ++
 3 files changed, 34 insertions(+)

Comments

Devin Heitmueller Feb. 7, 2019, 6:21 p.m.
> On Feb 7, 2019, at 1:11 PM, Mathieu Duponchelle <mathieu@centricular.com> wrote:
> +            if (side_data->size <= 96) {

Isn’t this calculation incorrect?  The max cc_count possible is 31 (0x1F), hence the max size should be 93.

> +                int i = 0;
> +
> +                put_header (s, USER_START_CODE);
> +
> +                put_bits(&s->pb, 8, 'G');                   // user_identifier
> +                put_bits(&s->pb, 8, 'A');
> +                put_bits(&s->pb, 8, '9');
> +                put_bits(&s->pb, 8, '4');
> +                put_bits(&s->pb, 8, 3);                     // user_data_type_code
> +                put_bits(&s->pb, 8,
> +                    ((side_data->size / 3) & 0x1f) | 0x40); // flags, cc_count
> +                put_bits(&s->pb, 8, 0xff);                  // em_data


---
Devin Heitmueller - LTN Global Communications
dheitmueller@ltnglobal.com
Mathieu Duponchelle Feb. 7, 2019, 6:22 p.m.
On 2/7/19 7:21 PM, Devin Heitmueller wrote:
> Isn’t this calculation incorrect?  The max cc_count possible is 31 (0x1F), hence the max size should be 93.
>

True that, updating
Devin Heitmueller Feb. 7, 2019, 6:27 p.m.
> On Feb 7, 2019, at 1:22 PM, Mathieu Duponchelle <mathieu@centricular.com> wrote:
> 
> 
> 
> On 2/7/19 7:21 PM, Devin Heitmueller wrote:
>> Isn’t this calculation incorrect?  The max cc_count possible is 31 (0x1F), hence the max size should be 93.
>> 
> 
> True that, updating

Not to nitpick, but it might also be worthwhile to create some #define such as MAX_CC_COUNT and have the comparison be "MAX_CC_COUNT * 3”.  That makes clear where the magic value “91” came from, and the compiler will optimize out the multiply anyway since it’s a constant.

Devin

---
Devin Heitmueller - LTN Global Communications
dheitmueller@ltnglobal.com


> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Patch hide | download patch | download mbox

diff --git a/doc/encoders.texi b/doc/encoders.texi
index e86ae69cc5..378a2ca8eb 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -2574,6 +2574,9 @@  Specifies the video_format written into the sequence display extension
 indicating the source of the video pictures. The default is @samp{unspecified},
 can be @samp{component}, @samp{pal}, @samp{ntsc}, @samp{secam} or @samp{mac}.
 For maximum compatibility, use @samp{component}.
+@item a53cc @var{boolean}
+Import closed captions (which must be ATSC compatible format) into output.
+Only the mpeg2 and h264 decoders provide these. Default is 1 (on).
 @end table
 
 @section png
diff --git a/libavcodec/mpeg12enc.c b/libavcodec/mpeg12enc.c
index d0b458e34b..b01bdcc591 100644
--- a/libavcodec/mpeg12enc.c
+++ b/libavcodec/mpeg12enc.c
@@ -544,6 +544,35 @@  void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
         }
     }
 
+    if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->a53_cc) {
+        side_data = av_frame_get_side_data(s->current_picture_ptr->f,
+            AV_FRAME_DATA_A53_CC);
+        if (side_data) {
+            if (side_data->size <= 96) {
+                int i = 0;
+
+                put_header (s, USER_START_CODE);
+
+                put_bits(&s->pb, 8, 'G');                   // user_identifier
+                put_bits(&s->pb, 8, 'A');
+                put_bits(&s->pb, 8, '9');
+                put_bits(&s->pb, 8, '4');
+                put_bits(&s->pb, 8, 3);                     // user_data_type_code
+                put_bits(&s->pb, 8,
+                    ((side_data->size / 3) & 0x1f) | 0x40); // flags, cc_count
+                put_bits(&s->pb, 8, 0xff);                  // em_data
+
+                for (i = 0; i < side_data->size; i++)
+                    put_bits(&s->pb, 8, side_data->data[i]);
+
+                put_bits(&s->pb, 8, 0xff);                  // marker_bits
+            } else {
+                av_log(s->avctx, AV_LOG_WARNING,
+                    "Warning Closed Caption size can not exceed 96 bytes\n");
+            }
+        }
+    }
+
     s->mb_y = 0;
     ff_mpeg1_encode_slice_header(s);
 }
diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index bbc6b5646a..3e52f98390 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -455,6 +455,7 @@  typedef struct MpegEncContext {
     /* MPEG-2-specific - I wished not to have to support this mess. */
     int progressive_sequence;
     int mpeg_f_code[2][2];
+    int a53_cc;
 
     // picture structure defines are loaded from mpegutils.h
     int picture_structure;
@@ -663,6 +664,7 @@  FF_MPV_OPT_CMP_FUNC, \
 {"ps", "RTP payload size in bytes",                             FF_MPV_OFFSET(rtp_payload_size), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
 {"mepc", "Motion estimation bitrate penalty compensation (1.0 = 256)", FF_MPV_OFFSET(me_penalty_compensation), AV_OPT_TYPE_INT, {.i64 = 256 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
 {"mepre", "pre motion estimation", FF_MPV_OFFSET(me_pre), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
+{ "a53cc", "Use A53 Closed Captions (if available)", FF_MPV_OFFSET(a53_cc), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FF_MPV_OPT_FLAGS }, \
 
 extern const AVOption ff_mpv_generic_options[];