2 * Bitstream filter for unpacking DivX-style packed B-frames in MPEG-4 (divx_packed)
3 * Copyright (c) 2015 Andreas Cadhalpun <Andreas.Cadhalpun@googlemail.com>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "mpeg4video.h"
25 typedef struct UnpackBFramesBSFContext {
28 int updated_extradata;
29 } UnpackBFramesBSFContext;
31 /* search next start code */
32 static unsigned int find_startcode(const uint8_t *buf, int buf_size, int *pos)
34 unsigned int startcode = 0xFF;
36 for (; *pos < buf_size;) {
37 startcode = ((startcode << 8) | buf[*pos]) & 0xFFFFFFFF;
39 if ((startcode & 0xFFFFFF00) != 0x100)
40 continue; /* no startcode */
47 /* determine the position of the packed marker in the userdata,
48 * the number of VOPs and the position of the second VOP */
49 static void scan_buffer(const uint8_t *buf, int buf_size,
50 int *pos_p, int *nb_vop, int *pos_vop2) {
51 unsigned int startcode;
54 for (pos = 0; pos < buf_size;) {
55 startcode = find_startcode(buf, buf_size, &pos);
57 if (startcode == USER_DATA_STARTCODE && pos_p) {
58 /* check if the (DivX) userdata string ends with 'p' (packed) */
59 for (i = 0; i < 255 && pos + i + 1 < buf_size; i++) {
60 if (buf[pos + i] == 'p' && buf[pos + i + 1] == '\0') {
65 } else if (startcode == VOP_STARTCODE && nb_vop) {
67 if (*nb_vop == 2 && pos_vop2) {
68 *pos_vop2 = pos - 4; /* subtract 4 bytes startcode */
74 /* allocate new buffer and copy size bytes from src */
75 static uint8_t *create_new_buffer(const uint8_t *src, int size) {
76 uint8_t *dst = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
79 memcpy(dst, src, size);
80 memset(dst + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
86 static int mpeg4_unpack_bframes_filter(AVBitStreamFilterContext *bsfc,
87 AVCodecContext *avctx, const char *args,
88 uint8_t **poutbuf, int *poutbuf_size,
89 const uint8_t *buf, int buf_size,
92 UnpackBFramesBSFContext *ctx = bsfc->priv_data;
93 int pos_p = -1, nb_vop = 0, pos_vop2 = -1, ret = 0;
95 if (avctx->codec_id != AV_CODEC_ID_MPEG4) {
96 av_log(avctx, AV_LOG_ERROR,
97 "The mpeg4_unpack_bframes bitstream filter is only useful for mpeg4.\n");
98 return AVERROR(EINVAL);
101 if (!ctx->updated_extradata && avctx->extradata) {
103 scan_buffer(avctx->extradata, avctx->extradata_size, &pos_p_ext, NULL, NULL);
104 if (pos_p_ext >= 0) {
105 av_log(avctx, AV_LOG_DEBUG,
106 "Updating DivX userdata (remove trailing 'p') in extradata.\n");
107 avctx->extradata[pos_p_ext] = '\0';
109 ctx->updated_extradata = 1;
112 scan_buffer(buf, buf_size, &pos_p, &nb_vop, &pos_vop2);
113 av_log(avctx, AV_LOG_DEBUG, "Found %d VOP startcode(s) in this packet.\n", nb_vop);
116 if (ctx->b_frame_buf) {
117 av_log(avctx, AV_LOG_WARNING,
118 "Missing one N-VOP packet, discarding one B-frame.\n");
119 av_freep(&ctx->b_frame_buf);
120 ctx->b_frame_buf_size = 0;
122 /* store the packed B-frame in the BSFContext */
123 ctx->b_frame_buf_size = buf_size - pos_vop2;
124 ctx->b_frame_buf = create_new_buffer(buf + pos_vop2, ctx->b_frame_buf_size);
125 if (!ctx->b_frame_buf) {
126 ctx->b_frame_buf_size = 0;
127 return AVERROR(ENOMEM);
132 av_log(avctx, AV_LOG_WARNING,
133 "Found %d VOP headers in one packet, only unpacking one.\n", nb_vop);
136 if (nb_vop == 1 && ctx->b_frame_buf) {
137 /* use frame from BSFContext */
138 *poutbuf = ctx->b_frame_buf;
139 *poutbuf_size = ctx->b_frame_buf_size;
140 /* the output buffer is distinct from the input buffer */
142 if (buf_size <= MAX_NVOP_SIZE) {
144 av_log(avctx, AV_LOG_DEBUG, "Skipping N-VOP.\n");
145 ctx->b_frame_buf = NULL;
146 ctx->b_frame_buf_size = 0;
148 /* copy packet into BSFContext */
149 ctx->b_frame_buf_size = buf_size;
150 ctx->b_frame_buf = create_new_buffer(buf , buf_size);
151 if (!ctx->b_frame_buf) {
152 ctx->b_frame_buf_size = 0;
155 return AVERROR(ENOMEM);
158 } else if (nb_vop >= 2) {
159 /* use first frame of the packet */
160 *poutbuf = (uint8_t *) buf;
161 *poutbuf_size = pos_vop2;
162 } else if (pos_p >= 0) {
163 av_log(avctx, AV_LOG_DEBUG, "Updating DivX userdata (remove trailing 'p').\n");
164 *poutbuf_size = buf_size;
165 *poutbuf = create_new_buffer(buf, buf_size);
168 return AVERROR(ENOMEM);
170 /* remove 'p' (packed) from the end of the (DivX) userdata string */
171 (*poutbuf)[pos_p] = '\0';
172 /* the output buffer is distinct from the input buffer */
176 *poutbuf = (uint8_t *) buf;
177 *poutbuf_size = buf_size;
183 static void mpeg4_unpack_bframes_close(AVBitStreamFilterContext *bsfc)
185 UnpackBFramesBSFContext *ctx = bsfc->priv_data;
186 av_freep(&ctx->b_frame_buf);
189 AVBitStreamFilter ff_mpeg4_unpack_bframes_bsf = {
190 .name = "mpeg4_unpack_bframes",
191 .priv_data_size = sizeof(UnpackBFramesBSFContext),
192 .filter = mpeg4_unpack_bframes_filter,
193 .close = mpeg4_unpack_bframes_close