/src/ffmpeg/libavcodec/bsf/mpeg4_unpack_bframes.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
22 | | #include "bsf.h" |
23 | | #include "bsf_internal.h" |
24 | | #include "mpeg4videodefs.h" |
25 | | #include "startcode.h" |
26 | | |
27 | | typedef struct UnpackBFramesBSFContext { |
28 | | AVBufferRef *b_frame_ref; |
29 | | } UnpackBFramesBSFContext; |
30 | | |
31 | | /* determine the position of the packed marker in the userdata, |
32 | | * the number of VOPs and the position of the second VOP */ |
33 | | static void scan_buffer(const uint8_t *buf, int buf_size, |
34 | 13.2k | int *pos_p, int *nb_vop, int *pos_vop2) { |
35 | 13.2k | uint32_t startcode; |
36 | 13.2k | const uint8_t *end = buf + buf_size, *pos = buf; |
37 | | |
38 | 1.34M | while (pos < end) { |
39 | 1.32M | startcode = -1; |
40 | 1.32M | pos = avpriv_find_start_code(pos, end, &startcode); |
41 | | |
42 | 1.32M | if (startcode == USER_DATA_STARTCODE && pos_p) { |
43 | | /* check if the (DivX) userdata string ends with 'p' (packed) */ |
44 | 8.66M | for (int i = 0; i < 255 && pos + i + 1 < end; i++) { |
45 | 8.63M | if (pos[i] == 'p' && pos[i + 1] == '\0') { |
46 | 8.76k | *pos_p = pos + i - buf; |
47 | 8.76k | break; |
48 | 8.76k | } |
49 | 8.63M | } |
50 | 1.28M | } else if (startcode == VOP_STARTCODE && nb_vop) { |
51 | 1.11M | *nb_vop += 1; |
52 | 1.11M | if (*nb_vop == 2 && pos_vop2) { |
53 | 3.15k | *pos_vop2 = pos - buf - 4; /* subtract 4 bytes startcode */ |
54 | 3.15k | } |
55 | 1.11M | } |
56 | 1.32M | } |
57 | 13.2k | } |
58 | | |
59 | | static int mpeg4_unpack_bframes_filter(AVBSFContext *ctx, AVPacket *pkt) |
60 | 26.5k | { |
61 | 26.5k | UnpackBFramesBSFContext *s = ctx->priv_data; |
62 | 26.5k | int pos_p = -1, nb_vop = 0, pos_vop2 = -1, ret = 0; |
63 | | |
64 | 26.5k | ret = ff_bsf_get_packet_ref(ctx, pkt); |
65 | 26.5k | if (ret < 0) |
66 | 13.4k | return ret; |
67 | | |
68 | 13.0k | scan_buffer(pkt->data, pkt->size, &pos_p, &nb_vop, &pos_vop2); |
69 | 13.0k | av_log(ctx, AV_LOG_DEBUG, "Found %d VOP startcode(s) in this packet.\n", nb_vop); |
70 | | |
71 | 13.0k | if (pos_vop2 >= 0) { |
72 | 3.15k | if (s->b_frame_ref) { |
73 | 935 | av_log(ctx, AV_LOG_WARNING, |
74 | 935 | "Missing one N-VOP packet, discarding one B-frame.\n"); |
75 | 935 | av_buffer_unref(&s->b_frame_ref); |
76 | 935 | } |
77 | | /* store a reference to the packed B-frame's data in the BSFContext */ |
78 | 3.15k | s->b_frame_ref = av_buffer_ref(pkt->buf); |
79 | 3.15k | if (!s->b_frame_ref) { |
80 | 0 | ret = AVERROR(ENOMEM); |
81 | 0 | goto fail; |
82 | 0 | } |
83 | 3.15k | s->b_frame_ref->data = pkt->data + pos_vop2; |
84 | 3.15k | s->b_frame_ref->size = pkt->size - pos_vop2; |
85 | 3.15k | } |
86 | | |
87 | 13.0k | if (nb_vop > 2) { |
88 | 1.65k | av_log(ctx, AV_LOG_WARNING, |
89 | 1.65k | "Found %d VOP headers in one packet, only unpacking one.\n", nb_vop); |
90 | 1.65k | } |
91 | | |
92 | 13.0k | if (nb_vop == 1 && s->b_frame_ref) { |
93 | 885 | AVBufferRef *tmp = pkt->buf; |
94 | | |
95 | | /* make tmp accurately reflect the packet's data */ |
96 | 885 | tmp->data = pkt->data; |
97 | 885 | tmp->size = pkt->size; |
98 | | |
99 | | /* replace data in packet with stored data */ |
100 | 885 | pkt->buf = s->b_frame_ref; |
101 | 885 | pkt->data = s->b_frame_ref->data; |
102 | 885 | pkt->size = s->b_frame_ref->size; |
103 | | |
104 | | /* store reference to data into BSFContext */ |
105 | 885 | s->b_frame_ref = tmp; |
106 | | |
107 | 885 | if (s->b_frame_ref->size <= MAX_NVOP_SIZE) { |
108 | | /* N-VOP - discard stored data */ |
109 | 531 | av_log(ctx, AV_LOG_DEBUG, "Skipping N-VOP.\n"); |
110 | 531 | av_buffer_unref(&s->b_frame_ref); |
111 | 531 | } |
112 | 12.1k | } else if (nb_vop >= 2) { |
113 | | /* use first frame of the packet */ |
114 | 3.15k | pkt->size = pos_vop2; |
115 | 9.00k | } else if (pos_p >= 0) { |
116 | 849 | ret = av_packet_make_writable(pkt); |
117 | 849 | if (ret < 0) |
118 | 0 | goto fail; |
119 | 849 | av_log(ctx, AV_LOG_DEBUG, "Updating DivX userdata (remove trailing 'p').\n"); |
120 | | /* remove 'p' (packed) from the end of the (DivX) userdata string */ |
121 | 849 | pkt->data[pos_p] = '\0'; |
122 | 8.15k | } else { |
123 | | /* use packet as is */ |
124 | 8.15k | } |
125 | | |
126 | 13.0k | fail: |
127 | 13.0k | if (ret < 0) |
128 | 0 | av_packet_unref(pkt); |
129 | | |
130 | 13.0k | return ret; |
131 | 13.0k | } |
132 | | |
133 | | static int mpeg4_unpack_bframes_init(AVBSFContext *ctx) |
134 | 402 | { |
135 | 402 | if (ctx->par_in->extradata) { |
136 | 157 | int pos_p_ext = -1; |
137 | 157 | scan_buffer(ctx->par_in->extradata, ctx->par_in->extradata_size, &pos_p_ext, NULL, NULL); |
138 | 157 | if (pos_p_ext >= 0) { |
139 | 46 | av_log(ctx, AV_LOG_DEBUG, |
140 | 46 | "Updating DivX userdata (remove trailing 'p') in extradata.\n"); |
141 | 46 | ctx->par_out->extradata[pos_p_ext] = '\0'; |
142 | 46 | } |
143 | 157 | } |
144 | | |
145 | 402 | return 0; |
146 | 402 | } |
147 | | |
148 | | static void mpeg4_unpack_bframes_close_flush(AVBSFContext *bsfc) |
149 | 6.00k | { |
150 | 6.00k | UnpackBFramesBSFContext *ctx = bsfc->priv_data; |
151 | 6.00k | av_buffer_unref(&ctx->b_frame_ref); |
152 | 6.00k | } |
153 | | |
154 | | static const enum AVCodecID codec_ids[] = { |
155 | | AV_CODEC_ID_MPEG4, AV_CODEC_ID_NONE, |
156 | | }; |
157 | | |
158 | | const FFBitStreamFilter ff_mpeg4_unpack_bframes_bsf = { |
159 | | .p.name = "mpeg4_unpack_bframes", |
160 | | .p.codec_ids = codec_ids, |
161 | | .priv_data_size = sizeof(UnpackBFramesBSFContext), |
162 | | .init = mpeg4_unpack_bframes_init, |
163 | | .filter = mpeg4_unpack_bframes_filter, |
164 | | .flush = mpeg4_unpack_bframes_close_flush, |
165 | | .close = mpeg4_unpack_bframes_close_flush, |
166 | | }; |