Hi,

A fix for the missing in-band Sequence Headers from the QSV MPEG-2 HW Encoder.

Regards.
A.H.

---
From a0b8525e0ebfd1a3b91bad7a21cc9de5c5a01e0e Mon Sep 17 00:00:00 2001
From: Andreas Hakon <andreas.ha...@protonmail.com>
Date: Tue, 14 May 2019 11:07:10 +0100
Subject: [PATCH] libavformat/qsvenc: fix mpeg2 missing headers

The current implementation of the QSV MPEG-2 HW encoder writes the value
of MPEG-2 sequence headers in-band one time only. That is, in the first GOP
of the stream. This behavior generates a bitstream that is not suitable for
streaming or broadcasting.
This patch resolves this problem by reading these headers the first time 
they appear, and reinserting them back into each GOP with the rest of
the MPEG-2 headers.

Signed-off-by: Andreas Hakon <andreas.ha...@protonmail.com>
---
 libavcodec/qsvenc.c |   88 +++++++++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/qsvenc.h |    5 +++
 2 files changed, 93 insertions(+)

diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
index a03ab69..18d4540 100644
--- a/libavcodec/qsvenc.c
+++ b/libavcodec/qsvenc.c
@@ -39,6 +39,7 @@
 #include "qsv.h"
 #include "qsv_internal.h"
 #include "qsvenc.h"
+#include "mpegvideo.h"
 
 static const struct {
     mfxU16 profile;
@@ -998,6 +999,13 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext 
*q)
     int opaque_alloc = 0;
     int ret;
 
+    if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
+        q->section_state = 0;
+    }
+    else {
+        q->section_state = -1;
+    }
+
     q->param.AsyncDepth = q->async_depth;
 
     q->async_fifo = av_fifo_alloc(q->async_depth * qsv_fifo_item_size());
@@ -1296,6 +1304,10 @@ static int encode_frame(AVCodecContext *avctx, 
QSVEncContext *q,
     QSVFrame *qsv_frame = NULL;
     mfxEncodeCtrl* enc_ctrl = NULL;
     int ret;
+    int p_sec, p_bytes_left;
+    const uint8_t *p_buf;
+    const uint8_t *p_buf_end;
+    uint32_t start_code;
 
     if (frame) {
         ret = submit_frame(q, frame, &qsv_frame);
@@ -1388,6 +1400,82 @@ static int encode_frame(AVCodecContext *avctx, 
QSVEncContext *q,
                0 : ff_qsv_print_error(avctx, ret, "Error during encoding");
     }
 
+    // Patch MPEG-2 missing Sequence Sections
+    if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
+        (bs->FrameType & MFX_FRAMETYPE_I || bs->FrameType & MFX_FRAMETYPE_xI)) 
{
+        if (q->section_state == 0) {
+            av_log(avctx, AV_LOG_VERBOSE, "Searching for MPEG-2 initial 
Sequence Sections\n");
+            // LOAD Sequence sections
+            p_sec = 0;
+            p_buf = bs->Data;
+            p_buf_end = bs->Data + bs->DataLength;
+            while (p_buf < p_buf_end) {
+                start_code= -1;
+                p_buf = avpriv_find_start_code(p_buf, p_buf_end, &start_code);
+                p_bytes_left = p_buf_end - p_buf;
+                switch(start_code) {
+                case SEQ_START_CODE:
+                    if (p_bytes_left >= 7) {
+                        memcpy(q->seq_header, p_buf - 4, 13);
+                        p_sec += 1;
+                        av_log(avctx, AV_LOG_VERBOSE, "Found MPEG-2 
SEQ_START_CODE Section\n");
+                        if ((p_buf[7] & 0x1) == 1) {
+                            memcpy(q->matrix, p_buf + 8, 64);
+                            p_sec += 4;
+                            av_log(avctx, AV_LOG_VERBOSE, "Found MPEG-2 
Matrix\n");
+                        }
+                    }
+                    continue;
+                case EXT_START_CODE:
+                    if (p_bytes_left >= 5 && (p_buf[0] >> 4) == 0x01) {
+                        memcpy(q->seq_ext, p_buf - 4, 10);
+                        p_sec += 2;
+                        av_log(avctx, AV_LOG_VERBOSE, "Found MPEG-2 
EXT_START_CODE Section\n");
+                    }
+                    continue;
+                case -1:
+                    break;
+                default:
+                    if (start_code >= SLICE_MIN_START_CODE &&
+                        start_code <= SLICE_MAX_START_CODE)
+                        break;
+                    continue;
+                }
+                break;
+            }
+            switch (p_sec) {
+            case 3:
+                q->section_state = 1;  // SEQ_START_CODE & EXT_START_CODE
+                break;
+            case 7:
+                q->section_state = 2;  // SEQ_START_CODE+Matrix & 
EXT_START_CODE
+                break;
+            default:
+                q->section_state = 0;  // Insufficient data
+            }
+        } else {
+            // ADD stored sections
+            switch (q->section_state) {
+            case 1:
+                memmove(bs->Data + 23, bs->Data, bs->DataLength - 23);
+                bs->DataLength  += 23;
+                memcpy( bs->Data + 0 , q->seq_header, 13);
+                memcpy( bs->Data + 13, q->seq_ext,    10);
+                break;
+            case 2:
+                memmove(bs->Data + 87, bs->Data, bs->DataLength - 87);
+                bs->DataLength  += 87;
+                memcpy( bs->Data + 0 , q->seq_header, 13);
+                memcpy( bs->Data + 13, q->matrix,     64);
+                memcpy( bs->Data + 77, q->seq_ext,    10);
+                break;
+            default:
+                return ff_qsv_print_error(avctx, MFX_ERR_UNDEFINED_BEHAVIOR,
+                       "Impossible to Patch MPEG-2 missing Sequence Sections");
+            }
+        }
+    }
+
     if (ret == MFX_WRN_INCOMPATIBLE_VIDEO_PARAM && frame->interlaced_frame)
         print_interlace_msg(avctx, q);
 
diff --git a/libavcodec/qsvenc.h b/libavcodec/qsvenc.h
index f2f4d38..f2c9fba 100644
--- a/libavcodec/qsvenc.h
+++ b/libavcodec/qsvenc.h
@@ -177,6 +177,11 @@ typedef struct QSVEncContext {
     int low_power;
     int gpb;
 
+    int section_state;  // -1: not used; 0: uninitialized; 1: loaded without 
matrix; 2: loaded with matrix
+    uint8_t seq_header[13];
+    uint8_t matrix[64];
+    uint8_t seq_ext[10];
+
     int a53_cc;
 
 #if QSV_HAVE_MF
-- 
1.7.10.4

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

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to