1 /*
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>
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
26
30
31 /* determine the position of the packed marker in the userdata,
32 * the number of VOPs and the position of the second VOP */
34 int *pos_p, int *nb_vop, int *pos_vop2) {
35 uint32_t startcode;
36 const uint8_t *end = buf + buf_size, *
pos = buf;
37
39 startcode = -1;
41
43 /* check if the (DivX) userdata string ends with 'p' (packed) */
44 for (
int i = 0;
i < 255 &&
pos +
i + 1 < end;
i++) {
45 if (
pos[
i] ==
'p' &&
pos[
i + 1] ==
'0円') {
46 *pos_p =
pos +
i - buf;
47 break;
48 }
49 }
51 *nb_vop += 1;
52 if (*nb_vop == 2 && pos_vop2) {
53 *pos_vop2 =
pos - buf - 4;
/* subtract 4 bytes startcode */
54 }
55 }
56 }
57 }
58
60 {
62 int pos_p = -1, nb_vop = 0, pos_vop2 = -1,
ret = 0;
63
67
70
71 if (pos_vop2 >= 0) {
74 "Missing one N-VOP packet, discarding one B-frame.\n");
76 }
77 /* store a reference to the packed B-frame's data in the BSFContext */
79 if (!
s->b_frame_ref) {
82 }
83 s->b_frame_ref->data =
pkt->
data + pos_vop2;
84 s->b_frame_ref->size =
pkt->
size - pos_vop2;
85 }
86
87 if (nb_vop > 2) {
89 "Found %d VOP headers in one packet, only unpacking one.\n", nb_vop);
90 }
91
92 if (nb_vop == 1 &&
s->b_frame_ref) {
94
95 /* make tmp accurately reflect the packet's data */
98
99 /* replace data in packet with stored data */
103
104 /* store reference to data into BSFContext */
105 s->b_frame_ref =
tmp;
106
108 /* N-VOP - discard stored data */
111 }
112 } else if (nb_vop >= 2) {
113 /* use first frame of the packet */
115 } else if (pos_p >= 0) {
120 /* remove 'p' (packed) from the end of the (DivX) userdata string */
122 } else {
123 /* use packet as is */
124 }
125
129
131 }
132
134 {
135 if (
ctx->par_in->extradata) {
136 int pos_p_ext = -1;
138 if (pos_p_ext >= 0) {
140 "Updating DivX userdata (remove trailing 'p') in extradata.\n");
141 ctx->par_out->extradata[pos_p_ext] =
'0円';
142 }
143 }
144
145 return 0;
146 }
147
149 {
152 }
153
156 };
157
159 .
p.
name =
"mpeg4_unpack_bframes",
166 };