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
24 #include "mpeg4video.h"
26 typedef struct UnpackBFramesBSFContext {
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 + AV_INPUT_BUFFER_PADDING_SIZE);
79 memcpy(dst, src, size);
80 memset(dst + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
86 static int mpeg4_unpack_bframes_filter(AVBSFContext *ctx, AVPacket *out)
88 UnpackBFramesBSFContext *s = ctx->priv_data;
89 int pos_p = -1, nb_vop = 0, pos_vop2 = -1, ret = 0;
92 ret = ff_bsf_get_packet(ctx, &in);
96 scan_buffer(in->data, in->size, &pos_p, &nb_vop, &pos_vop2);
97 av_log(ctx, AV_LOG_DEBUG, "Found %d VOP startcode(s) in this packet.\n", nb_vop);
100 if (s->b_frame_buf) {
101 av_log(ctx, AV_LOG_WARNING,
102 "Missing one N-VOP packet, discarding one B-frame.\n");
103 av_freep(&s->b_frame_buf);
104 s->b_frame_buf_size = 0;
106 /* store the packed B-frame in the BSFContext */
107 s->b_frame_buf_size = in->size - pos_vop2;
108 s->b_frame_buf = create_new_buffer(in->data + pos_vop2, s->b_frame_buf_size);
109 if (!s->b_frame_buf) {
110 s->b_frame_buf_size = 0;
112 return AVERROR(ENOMEM);
117 av_log(ctx, AV_LOG_WARNING,
118 "Found %d VOP headers in one packet, only unpacking one.\n", nb_vop);
121 if (nb_vop == 1 && s->b_frame_buf) {
122 /* use frame from BSFContext */
123 av_packet_from_data(out, s->b_frame_buf, s->b_frame_buf_size);
124 if (in->size <= MAX_NVOP_SIZE) {
126 av_log(ctx, AV_LOG_DEBUG, "Skipping N-VOP.\n");
127 s->b_frame_buf = NULL;
128 s->b_frame_buf_size = 0;
130 /* copy packet into BSFContext */
131 s->b_frame_buf_size = in->size;
132 s->b_frame_buf = create_new_buffer(in->data, in->size);
133 if (!s->b_frame_buf) {
134 s->b_frame_buf_size = 0;
135 av_packet_unref(out);
137 return AVERROR(ENOMEM);
140 } else if (nb_vop >= 2) {
141 /* use first frame of the packet */
142 av_packet_move_ref(out, in);
143 out->size = pos_vop2;
144 } else if (pos_p >= 0) {
145 av_log(ctx, AV_LOG_DEBUG, "Updating DivX userdata (remove trailing 'p').\n");
146 av_packet_move_ref(out, in);
147 /* remove 'p' (packed) from the end of the (DivX) userdata string */
148 out->data[pos_p] = '\0';
151 av_packet_move_ref(out, in);
159 static int mpeg4_unpack_bframes_init(AVBSFContext *ctx)
161 if (ctx->par_in->extradata) {
163 scan_buffer(ctx->par_in->extradata, ctx->par_in->extradata_size, &pos_p_ext, NULL, NULL);
164 if (pos_p_ext >= 0) {
165 av_log(ctx, AV_LOG_DEBUG,
166 "Updating DivX userdata (remove trailing 'p') in extradata.\n");
167 ctx->par_out->extradata[pos_p_ext] = '\0';
174 static void mpeg4_unpack_bframes_close(AVBSFContext *bsfc)
176 UnpackBFramesBSFContext *ctx = bsfc->priv_data;
177 av_freep(&ctx->b_frame_buf);
180 static const enum AVCodecID codec_ids[] = {
181 AV_CODEC_ID_MPEG4, AV_CODEC_ID_NONE,
184 const AVBitStreamFilter ff_mpeg4_unpack_bframes_bsf = {
185 .name = "mpeg4_unpack_bframes",
186 .priv_data_size = sizeof(UnpackBFramesBSFContext),
187 .init = mpeg4_unpack_bframes_init,
188 .filter = mpeg4_unpack_bframes_filter,
189 .close = mpeg4_unpack_bframes_close,
190 .codec_ids = codec_ids,