diff mbox

[FFmpeg-devel] avcodec/vaapi_encode_h264: add support for a/53 closed caption sei

Message ID 20190801194259.52199-1-ffmpeg@tmm1.net
State Superseded
Headers show

Commit Message

Aman Karmani Aug. 1, 2019, 7:42 p.m. UTC
From: Aman Gupta <aman@tmm1.net>

Signed-off-by: Aman Gupta <aman@tmm1.net>
---
 libavcodec/vaapi_encode_h264.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
diff mbox

Patch

diff --git a/libavcodec/vaapi_encode_h264.c b/libavcodec/vaapi_encode_h264.c
index f4965d8b09..f66e483b7f 100644
--- a/libavcodec/vaapi_encode_h264.c
+++ b/libavcodec/vaapi_encode_h264.c
@@ -39,6 +39,7 @@  enum {
     SEI_TIMING         = 0x01,
     SEI_IDENTIFIER     = 0x02,
     SEI_RECOVERY_POINT = 0x04,
+    SEI_A53_CC         = 0x08,
 };
 
 // Random (version 4) ISO 11578 UUID.
@@ -96,6 +97,7 @@  typedef struct VAAPIEncodeH264Context {
     H264RawSEIBufferingPeriod      sei_buffering_period;
     H264RawSEIPicTiming            sei_pic_timing;
     H264RawSEIRecoveryPoint        sei_recovery_point;
+    H264RawSEIUserDataRegistered   sei_a53_cc;
     H264RawSEIUserDataUnregistered sei_identifier;
     char                          *sei_identifier_string;
 
@@ -251,6 +253,11 @@  static int vaapi_encode_h264_write_extra_header(AVCodecContext *avctx,
             sei->payload[i].payload.recovery_point = priv->sei_recovery_point;
             ++i;
         }
+        if (priv->sei_needed & SEI_A53_CC) {
+            sei->payload[i].payload_type = H264_SEI_TYPE_USER_DATA_REGISTERED;
+            sei->payload[i].payload.user_data_registered = priv->sei_a53_cc;
+            ++i;
+        }
 
         sei->payload_count = i;
         av_assert0(sei->payload_count > 0);
@@ -626,7 +633,8 @@  static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
     VAAPIEncodePicture              *prev = pic->prev;
     VAAPIEncodeH264Picture         *hprev = prev ? prev->priv_data : NULL;
     VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
-    int i;
+    AVFrameSideData                 *side_data = NULL;
+    int i, err;
 
     if (pic->type == PICTURE_TYPE_IDR) {
         av_assert0(pic->display_order == pic->encode_order);
@@ -700,6 +708,27 @@  static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
         priv->sei_needed |= SEI_RECOVERY_POINT;
     }
 
+    side_data = av_frame_get_side_data(pic->input_image, AV_FRAME_DATA_A53_CC);
+    if (side_data && side_data->size) {
+        priv->sei_a53_cc.itu_t_t35_country_code = 181;
+        priv->sei_a53_cc.data_length = side_data->size + 9;
+        err = av_reallocp(&priv->sei_a53_cc.data, priv->sei_a53_cc.data_length);
+        if (err < 0)
+            return err;
+        priv->sei_a53_cc.data[0] = 0;
+        priv->sei_a53_cc.data[1] = 49;
+        priv->sei_a53_cc.data[2] = 'G';
+        priv->sei_a53_cc.data[3] = 'A';
+        priv->sei_a53_cc.data[4] = '9';
+        priv->sei_a53_cc.data[5] = '4';
+        priv->sei_a53_cc.data[6] = 3;
+        priv->sei_a53_cc.data[7] = 0xc0 | (side_data->size/3);
+        priv->sei_a53_cc.data[8] = 255;
+        memcpy(priv->sei_a53_cc.data+9, side_data->data, side_data->size);
+
+        priv->sei_needed |= SEI_A53_CC;
+    }
+
     vpic->CurrPic = (VAPictureH264) {
         .picture_id          = pic->recon_surface,
         .frame_idx           = hpic->frame_num,
@@ -1245,6 +1274,7 @@  static av_cold int vaapi_encode_h264_close(AVCodecContext *avctx)
     ff_cbs_fragment_free(priv->cbc, &priv->current_access_unit);
     ff_cbs_close(&priv->cbc);
     av_freep(&priv->sei_identifier_string);
+    av_freep(&priv->sei_a53_cc.data);
 
     return ff_vaapi_encode_close(avctx);
 }