[FFmpeg-devel] [PATCH] lavc/mediacodecdec_h264: use ff_h264_decode_extradata to extract PPS/SPS
Matthieu Bouron
matthieu.bouron at gmail.com
Fri Jun 10 15:08:48 CEST 2016
From: Matthieu Bouron <matthieu.bouron at stupeflix.com>
Fixes playback of HLS streams on MediaTek devices which requires PPS/SPS
to be set in their respective csd-{0,1} buffers.
---
Hello,
The attached patch fixes playback of HLS streams on MediaTek devices which
requires PPS/SPS to be set in their respetive csd-{0,1} buffers (instead of
having sps+pps in the csd-0 which works on other devices).
I'm not sure if I can use the ff_h264_decode_extradata this way (or at least
initialize the H264Context with zeroes minus the avctx field).
Matthieu
---
configure | 2 +-
libavcodec/mediacodecdec_h264.c | 140 +++++++++-------------------------------
2 files changed, 30 insertions(+), 112 deletions(-)
diff --git a/configure b/configure
index 7c463a5..508affe 100755
--- a/configure
+++ b/configure
@@ -2544,7 +2544,7 @@ h264_d3d11va_hwaccel_select="h264_decoder"
h264_dxva2_hwaccel_deps="dxva2"
h264_dxva2_hwaccel_select="h264_decoder"
h264_mediacodec_decoder_deps="mediacodec"
-h264_mediacodec_decoder_select="h264_mp4toannexb_bsf h264_parser"
+h264_mediacodec_decoder_select="h264_mp4toannexb_bsf h264_decoder h264_parser"
h264_mmal_decoder_deps="mmal"
h264_mmal_decoder_select="mmal"
h264_mmal_hwaccel_deps="mmal"
diff --git a/libavcodec/mediacodecdec_h264.c b/libavcodec/mediacodecdec_h264.c
index 52e48ae..69e9122 100644
--- a/libavcodec/mediacodecdec_h264.c
+++ b/libavcodec/mediacodecdec_h264.c
@@ -32,6 +32,7 @@
#include "libavutil/atomic.h"
#include "avcodec.h"
+#include "h264.h"
#include "internal.h"
#include "mediacodecdec.h"
#include "mediacodec_wrapper.h"
@@ -50,104 +51,6 @@ typedef struct MediaCodecH264DecContext {
} MediaCodecH264DecContext;
-static int h264_extradata_to_annexb_sps_pps(AVCodecContext *avctx,
- uint8_t **extradata_annexb, int *extradata_annexb_size,
- int *sps_offset, int *sps_size,
- int *pps_offset, int *pps_size)
-{
- uint16_t unit_size;
- uint64_t total_size = 0;
-
- uint8_t i, j, unit_nb;
- uint8_t sps_seen = 0;
- uint8_t pps_seen = 0;
-
- const uint8_t *extradata;
- static const uint8_t nalu_header[4] = { 0x00, 0x00, 0x00, 0x01 };
-
- if (avctx->extradata_size < 8) {
- av_log(avctx, AV_LOG_ERROR,
- "Too small extradata size, corrupted stream or invalid MP4/AVCC bitstream\n");
- return AVERROR(EINVAL);
- }
-
- *extradata_annexb = NULL;
- *extradata_annexb_size = 0;
-
- *sps_offset = *sps_size = 0;
- *pps_offset = *pps_size = 0;
-
- extradata = avctx->extradata + 4;
-
- /* skip length size */
- extradata++;
-
- for (j = 0; j < 2; j ++) {
-
- if (j == 0) {
- /* number of sps unit(s) */
- unit_nb = *extradata++ & 0x1f;
- } else {
- /* number of pps unit(s) */
- unit_nb = *extradata++;
- }
-
- for (i = 0; i < unit_nb; i++) {
- int err;
-
- unit_size = AV_RB16(extradata);
- total_size += unit_size + 4;
-
- if (total_size > INT_MAX) {
- av_log(avctx, AV_LOG_ERROR,
- "Too big extradata size, corrupted stream or invalid MP4/AVCC bitstream\n");
- av_freep(extradata_annexb);
- return AVERROR(EINVAL);
- }
-
- if (extradata + 2 + unit_size > avctx->extradata + avctx->extradata_size) {
- av_log(avctx, AV_LOG_ERROR, "Packet header is not contained in global extradata, "
- "corrupted stream or invalid MP4/AVCC bitstream\n");
- av_freep(extradata_annexb);
- return AVERROR(EINVAL);
- }
-
- if ((err = av_reallocp(extradata_annexb, total_size)) < 0) {
- return err;
- }
-
- memcpy(*extradata_annexb + total_size - unit_size - 4, nalu_header, 4);
- memcpy(*extradata_annexb + total_size - unit_size, extradata + 2, unit_size);
- extradata += 2 + unit_size;
- }
-
- if (unit_nb) {
- if (j == 0) {
- sps_seen = 1;
- *sps_size = total_size;
- } else {
- pps_seen = 1;
- *pps_size = total_size - *sps_size;
- *pps_offset = *sps_size;
- }
- }
- }
-
- *extradata_annexb_size = total_size;
-
- if (!sps_seen)
- av_log(avctx, AV_LOG_WARNING,
- "Warning: SPS NALU missing or invalid. "
- "The resulting stream may not play.\n");
-
- if (!pps_seen)
- av_log(avctx, AV_LOG_WARNING,
- "Warning: PPS NALU missing or invalid. "
- "The resulting stream may not play.\n");
-
- return 0;
-}
-
static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
{
MediaCodecH264DecContext *s = avctx->priv_data;
@@ -164,7 +67,12 @@ static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
{
+ int i;
int ret;
+ PPS *pps = NULL;
+ SPS *sps = NULL;
+
+ H264Context h = { .avctx = avctx };
FFAMediaFormat *format = NULL;
MediaCodecH264DecContext *s = avctx->priv_data;
@@ -179,24 +87,31 @@ static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
ff_AMediaFormat_setInt32(format, "width", avctx->width);
ff_AMediaFormat_setInt32(format, "height", avctx->height);
- if (avctx->extradata[0] == 1) {
- uint8_t *extradata = NULL;
- int extradata_size = 0;
-
- int sps_offset, sps_size;
- int pps_offset, pps_size;
+ ret = ff_h264_decode_extradata(&h, avctx->extradata, avctx->extradata_size);
+ if (ret < 0) {
+ goto done;
+ }
- if ((ret = h264_extradata_to_annexb_sps_pps(avctx, &extradata, &extradata_size,
- &sps_offset, &sps_size, &pps_offset, &pps_size)) < 0) {
- goto done;
+ for (i = 0; i < MAX_PPS_COUNT; i++) {
+ if (h.pps_buffers[i]) {
+ pps = h.pps_buffers[i];
+ break;
}
+ }
- ff_AMediaFormat_setBuffer(format, "csd-0", extradata + sps_offset, sps_size);
- ff_AMediaFormat_setBuffer(format, "csd-1", extradata + pps_offset, pps_size);
+ if (pps) {
+ if (h.sps_buffers[pps->sps_id]) {
+ sps = h.sps_buffers[pps->sps_id];
+ }
+ }
- av_freep(&extradata);
+ if (pps && sps) {
+ ff_AMediaFormat_setBuffer(format, "csd-0", sps->data, sps->data_size);
+ ff_AMediaFormat_setBuffer(format, "csd-1", pps->data, pps->data_size);
} else {
- ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
+ av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
+ ret = AVERROR_EXTERNAL;
+ goto done;
}
if ((ret = ff_mediacodec_dec_init(avctx, &s->ctx, CODEC_MIME, format)) < 0) {
@@ -236,6 +151,9 @@ done:
if (ret < 0) {
mediacodec_decode_close(avctx);
}
+
+ ff_h264_free_context(&h);
+
return ret;
}
--
2.8.3
More information about the ffmpeg-devel
mailing list