cbs: Add buffer padding when splitting fragments
[libav.git] / libavcodec / cbs_mpeg2.c
1 /*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "libavutil/avassert.h"
20
21 #include "cbs.h"
22 #include "cbs_internal.h"
23 #include "cbs_mpeg2.h"
24 #include "internal.h"
25
26
27 #define HEADER(name) do { \
28 ff_cbs_trace_header(ctx, name); \
29 } while (0)
30
31 #define CHECK(call) do { \
32 err = (call); \
33 if (err < 0) \
34 return err; \
35 } while (0)
36
37 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
38 #define FUNC_MPEG2(rw, name) FUNC_NAME(rw, mpeg2, name)
39 #define FUNC(name) FUNC_MPEG2(READWRITE, name)
40
41
42 #define READ
43 #define READWRITE read
44 #define RWContext BitstreamContext
45
46 #define xui(width, name, var) do { \
47 uint32_t value = 0; \
48 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
49 &value, 0, (1 << width) - 1)); \
50 var = value; \
51 } while (0)
52
53 #define ui(width, name) \
54 xui(width, name, current->name)
55
56 #define marker_bit() do { \
57 av_unused int one = 1; \
58 CHECK(ff_cbs_read_unsigned(ctx, rw, 1, "marker_bit", &one, 1, 1)); \
59 } while (0)
60
61 #define nextbits(width, compare, var) \
62 (bitstream_bits_left(rw) >= width && \
63 (var = bitstream_peek(rw, width)) == (compare))
64
65 #include "cbs_mpeg2_syntax_template.c"
66
67 #undef READ
68 #undef READWRITE
69 #undef RWContext
70 #undef xui
71 #undef ui
72 #undef marker_bit
73 #undef nextbits
74
75
76 #define WRITE
77 #define READWRITE write
78 #define RWContext PutBitContext
79
80 #define xui(width, name, var) do { \
81 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
82 var, 0, (1 << width) - 1)); \
83 } while (0)
84
85 #define ui(width, name) \
86 xui(width, name, current->name)
87
88 #define marker_bit() do { \
89 CHECK(ff_cbs_write_unsigned(ctx, rw, 1, "marker_bit", 1, 1, 1)); \
90 } while (0)
91
92 #define nextbits(width, compare, var) (var)
93
94 #include "cbs_mpeg2_syntax_template.c"
95
96 #undef READ
97 #undef READWRITE
98 #undef RWContext
99 #undef xui
100 #undef ui
101 #undef marker_bit
102 #undef nextbits
103
104
105 static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
106 CodedBitstreamFragment *frag,
107 int header)
108 {
109 const uint8_t *start, *end;
110 uint8_t *unit_data;
111 uint32_t start_code = -1, next_start_code = -1;
112 size_t unit_size;
113 int err, i, unit_type;
114
115 start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
116 &start_code);
117 for (i = 0;; i++) {
118 end = avpriv_find_start_code(start, frag->data + frag->data_size,
119 &next_start_code);
120
121 unit_type = start_code & 0xff;
122
123 // The start and end pointers point at to the byte following the
124 // start_code_identifier in the start code that they found.
125 if (end == frag->data + frag->data_size) {
126 // We didn't find a start code, so this is the final unit.
127 unit_size = end - (start - 1);
128 } else {
129 // Unit runs from start to the beginning of the start code
130 // pointed to by end (including any padding zeroes).
131 unit_size = (end - 4) - (start - 1);
132 }
133
134 unit_data = av_malloc(unit_size + AV_INPUT_BUFFER_PADDING_SIZE);
135 if (!unit_data)
136 return AVERROR(ENOMEM);
137 memcpy(unit_data, start - 1, unit_size);
138 memset(unit_data + unit_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
139
140 err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type,
141 unit_data, unit_size);
142 if (err < 0) {
143 av_freep(&unit_data);
144 return err;
145 }
146
147 if (end == frag->data + frag->data_size)
148 break;
149
150 start_code = next_start_code;
151 start = end;
152 }
153
154 return 0;
155 }
156
157 static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx,
158 CodedBitstreamUnit *unit)
159 {
160 BitstreamContext bc;
161 int err;
162
163 err = bitstream_init(&bc, unit->data, 8 * unit->data_size);
164 if (err < 0)
165 return err;
166
167 if (MPEG2_START_IS_SLICE(unit->type)) {
168 MPEG2RawSlice *slice;
169 int pos, len;
170
171 slice = av_mallocz(sizeof(*slice));
172 if (!slice)
173 return AVERROR(ENOMEM);
174 err = cbs_mpeg2_read_slice_header(ctx, &bc, &slice->header);
175 if (err < 0) {
176 av_free(slice);
177 return err;
178 }
179
180 pos = bitstream_tell(&bc);
181 len = unit->data_size;
182
183 slice->data_size = len - pos / 8;
184 slice->data = av_malloc(slice->data_size);
185 if (!slice->data) {
186 av_free(slice);
187 return AVERROR(ENOMEM);
188 }
189
190 memcpy(slice->data,
191 unit->data + pos / 8, slice->data_size);
192 slice->data_bit_start = pos % 8;
193
194 unit->content = slice;
195
196 } else {
197 switch (unit->type) {
198 #define START(start_code, type, func) \
199 case start_code: \
200 { \
201 type *header; \
202 header = av_mallocz(sizeof(*header)); \
203 if (!header) \
204 return AVERROR(ENOMEM); \
205 err = cbs_mpeg2_read_ ## func(ctx, &bc, header); \
206 if (err < 0) { \
207 av_free(header); \
208 return err; \
209 } \
210 unit->content = header; \
211 } \
212 break;
213 START(0x00, MPEG2RawPictureHeader, picture_header);
214 START(0xb2, MPEG2RawUserData, user_data);
215 START(0xb3, MPEG2RawSequenceHeader, sequence_header);
216 START(0xb5, MPEG2RawExtensionData, extension_data);
217 START(0xb8, MPEG2RawGroupOfPicturesHeader, group_of_pictures_header);
218 #undef START
219 default:
220 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unknown start code %02x.\n",
221 unit->type);
222 return AVERROR_INVALIDDATA;
223 }
224 }
225
226 return 0;
227 }
228
229 static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx,
230 CodedBitstreamUnit *unit,
231 PutBitContext *pbc)
232 {
233 int err;
234
235 switch (unit->type) {
236 #define START(start_code, type, func) \
237 case start_code: \
238 err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
239 break;
240 START(0x00, MPEG2RawPictureHeader, picture_header);
241 START(0xb2, MPEG2RawUserData, user_data);
242 START(0xb3, MPEG2RawSequenceHeader, sequence_header);
243 START(0xb5, MPEG2RawExtensionData, extension_data);
244 START(0xb8, MPEG2RawGroupOfPicturesHeader, group_of_pictures_header);
245 #undef START
246 default:
247 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
248 "code %02x.\n", unit->type);
249 return AVERROR_PATCHWELCOME;
250 }
251
252 return err;
253 }
254
255 static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx,
256 CodedBitstreamUnit *unit,
257 PutBitContext *pbc)
258 {
259 MPEG2RawSlice *slice = unit->content;
260 BitstreamContext bc;
261 size_t bits_left;
262 int err;
263
264 err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
265 if (err < 0)
266 return err;
267
268 if (slice->data) {
269 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
270 return AVERROR(ENOSPC);
271
272 bitstream_init(&bc, slice->data, slice->data_size * 8);
273 bitstream_skip(&bc, slice->data_bit_start);
274
275 while (bitstream_bits_left(&bc) > 15)
276 put_bits(pbc, 16, bitstream_read(&bc, 16));
277
278 bits_left = bitstream_bits_left(&bc);
279 put_bits(pbc, bits_left, bitstream_read(&bc, bits_left));
280
281 // Align with zeroes.
282 while (put_bits_count(pbc) % 8 != 0)
283 put_bits(pbc, 1, 0);
284 }
285
286 return 0;
287 }
288
289 static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
290 CodedBitstreamUnit *unit)
291 {
292 CodedBitstreamMPEG2Context *priv = ctx->priv_data;
293 PutBitContext pbc;
294 int err;
295
296 if (!priv->write_buffer) {
297 // Initial write buffer size is 1MB.
298 priv->write_buffer_size = 1024 * 1024;
299
300 reallocate_and_try_again:
301 err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
302 if (err < 0) {
303 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
304 "sufficiently large write buffer (last attempt "
305 "%zu bytes).\n", priv->write_buffer_size);
306 return err;
307 }
308 }
309
310 init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
311
312 if (unit->type >= 0x01 && unit->type <= 0xaf)
313 err = cbs_mpeg2_write_slice(ctx, unit, &pbc);
314 else
315 err = cbs_mpeg2_write_header(ctx, unit, &pbc);
316
317 if (err == AVERROR(ENOSPC)) {
318 // Overflow.
319 priv->write_buffer_size *= 2;
320 goto reallocate_and_try_again;
321 }
322 if (err < 0) {
323 // Write failed for some other reason.
324 return err;
325 }
326
327 if (put_bits_count(&pbc) % 8)
328 unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
329 else
330 unit->data_bit_padding = 0;
331
332 unit->data_size = (put_bits_count(&pbc) + 7) / 8;
333 flush_put_bits(&pbc);
334
335 err = av_reallocp(&unit->data, unit->data_size);
336 if (err < 0)
337 return err;
338
339 memcpy(unit->data, priv->write_buffer, unit->data_size);
340
341 return 0;
342 }
343
344 static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
345 CodedBitstreamFragment *frag)
346 {
347 uint8_t *data;
348 size_t size, dp, sp;
349 int i;
350
351 size = 0;
352 for (i = 0; i < frag->nb_units; i++)
353 size += 3 + frag->units[i].data_size;
354
355 data = av_malloc(size);
356 if (!data)
357 return AVERROR(ENOMEM);
358
359 dp = 0;
360 for (i = 0; i < frag->nb_units; i++) {
361 CodedBitstreamUnit *unit = &frag->units[i];
362
363 data[dp++] = 0;
364 data[dp++] = 0;
365 data[dp++] = 1;
366
367 for (sp = 0; sp < unit->data_size; sp++)
368 data[dp++] = unit->data[sp];
369 }
370
371 av_assert0(dp == size);
372
373 frag->data = data;
374 frag->data_size = size;
375
376 return 0;
377 }
378
379 static void cbs_mpeg2_free_unit(CodedBitstreamUnit *unit)
380 {
381 if (MPEG2_START_IS_SLICE(unit->type)) {
382 MPEG2RawSlice *slice = unit->content;
383 av_freep(&slice->data);
384 av_freep(&slice->header.extra_information);
385 } else if (unit->type == MPEG2_START_USER_DATA) {
386 MPEG2RawUserData *user = unit->content;
387 av_freep(&user->user_data);
388 }
389 av_freep(&unit->content);
390 }
391
392 static void cbs_mpeg2_close(CodedBitstreamContext *ctx)
393 {
394 CodedBitstreamMPEG2Context *priv = ctx->priv_data;
395
396 av_freep(&priv->write_buffer);
397 }
398
399 const CodedBitstreamType ff_cbs_type_mpeg2 = {
400 .codec_id = AV_CODEC_ID_MPEG2VIDEO,
401
402 .priv_data_size = sizeof(CodedBitstreamMPEG2Context),
403
404 .split_fragment = &cbs_mpeg2_split_fragment,
405 .read_unit = &cbs_mpeg2_read_unit,
406 .write_unit = &cbs_mpeg2_write_unit,
407 .assemble_fragment = &cbs_mpeg2_assemble_fragment,
408
409 .free_unit = &cbs_mpeg2_free_unit,
410 .close = &cbs_mpeg2_close,
411 };