cbs: Always check for bitstream end before reading
[libav.git] / libavcodec / cbs_mpeg2.c
CommitLineData
2bc9ba8d
MT
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
44cde38c
MT
61#define nextbits(width, compare, var) \
62 (bitstream_bits_left(rw) >= width && \
63 (var = bitstream_peek(rw, width)) == (compare))
2bc9ba8d
MT
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
105static 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);
135 if (!unit_data)
136 return AVERROR(ENOMEM);
137 memcpy(unit_data, start - 1, unit_size);
138
139 err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type,
140 unit_data, unit_size);
141 if (err < 0) {
142 av_freep(&unit_data);
143 return err;
144 }
145
146 if (end == frag->data + frag->data_size)
147 break;
148
149 start_code = next_start_code;
150 start = end;
151 }
152
153 return 0;
154}
155
156static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx,
157 CodedBitstreamUnit *unit)
158{
159 BitstreamContext bc;
160 int err;
161
162 err = bitstream_init(&bc, unit->data, 8 * unit->data_size);
163 if (err < 0)
164 return err;
165
166 if (MPEG2_START_IS_SLICE(unit->type)) {
167 MPEG2RawSlice *slice;
168 int pos, len;
169
170 slice = av_mallocz(sizeof(*slice));
171 if (!slice)
172 return AVERROR(ENOMEM);
173 err = cbs_mpeg2_read_slice_header(ctx, &bc, &slice->header);
174 if (err < 0) {
175 av_free(slice);
176 return err;
177 }
178
179 pos = bitstream_tell(&bc);
180 len = unit->data_size;
181
182 slice->data_size = len - pos / 8;
183 slice->data = av_malloc(slice->data_size);
184 if (!slice->data) {
185 av_free(slice);
186 return AVERROR(ENOMEM);
187 }
188
189 memcpy(slice->data,
190 unit->data + pos / 8, slice->data_size);
191 slice->data_bit_start = pos % 8;
192
193 unit->content = slice;
194
195 } else {
196 switch (unit->type) {
197#define START(start_code, type, func) \
198 case start_code: \
199 { \
200 type *header; \
201 header = av_mallocz(sizeof(*header)); \
202 if (!header) \
203 return AVERROR(ENOMEM); \
204 err = cbs_mpeg2_read_ ## func(ctx, &bc, header); \
205 if (err < 0) { \
206 av_free(header); \
207 return err; \
208 } \
209 unit->content = header; \
210 } \
211 break;
212 START(0x00, MPEG2RawPictureHeader, picture_header);
213 START(0xb2, MPEG2RawUserData, user_data);
214 START(0xb3, MPEG2RawSequenceHeader, sequence_header);
215 START(0xb5, MPEG2RawExtensionData, extension_data);
216 START(0xb8, MPEG2RawGroupOfPicturesHeader, group_of_pictures_header);
217#undef START
218 default:
219 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unknown start code %02x.\n",
220 unit->type);
221 return AVERROR_INVALIDDATA;
222 }
223 }
224
225 return 0;
226}
227
228static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx,
229 CodedBitstreamUnit *unit,
230 PutBitContext *pbc)
231{
232 int err;
233
234 switch (unit->type) {
235#define START(start_code, type, func) \
236 case start_code: \
237 err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
238 break;
239 START(0x00, MPEG2RawPictureHeader, picture_header);
240 START(0xb2, MPEG2RawUserData, user_data);
241 START(0xb3, MPEG2RawSequenceHeader, sequence_header);
242 START(0xb5, MPEG2RawExtensionData, extension_data);
243 START(0xb8, MPEG2RawGroupOfPicturesHeader, group_of_pictures_header);
244#undef START
245 default:
246 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
247 "code %02x.\n", unit->type);
248 return AVERROR_PATCHWELCOME;
249 }
250
251 return err;
252}
253
254static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx,
255 CodedBitstreamUnit *unit,
256 PutBitContext *pbc)
257{
258 MPEG2RawSlice *slice = unit->content;
259 BitstreamContext bc;
260 size_t bits_left;
261 int err;
262
263 err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
264 if (err < 0)
265 return err;
266
267 if (slice->data) {
268 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
269 return AVERROR(ENOSPC);
270
271 bitstream_init(&bc, slice->data, slice->data_size * 8);
272 bitstream_skip(&bc, slice->data_bit_start);
273
274 while (bitstream_bits_left(&bc) > 15)
275 put_bits(pbc, 16, bitstream_read(&bc, 16));
276
277 bits_left = bitstream_bits_left(&bc);
278 put_bits(pbc, bits_left, bitstream_read(&bc, bits_left));
279
280 // Align with zeroes.
281 while (put_bits_count(pbc) % 8 != 0)
282 put_bits(pbc, 1, 0);
283 }
284
285 return 0;
286}
287
288static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
289 CodedBitstreamUnit *unit)
290{
291 CodedBitstreamMPEG2Context *priv = ctx->priv_data;
292 PutBitContext pbc;
293 int err;
294
295 if (!priv->write_buffer) {
296 // Initial write buffer size is 1MB.
297 priv->write_buffer_size = 1024 * 1024;
298
299 reallocate_and_try_again:
300 err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
301 if (err < 0) {
302 av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
303 "sufficiently large write buffer (last attempt "
304 "%zu bytes).\n", priv->write_buffer_size);
305 return err;
306 }
307 }
308
309 init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
310
311 if (unit->type >= 0x01 && unit->type <= 0xaf)
312 err = cbs_mpeg2_write_slice(ctx, unit, &pbc);
313 else
314 err = cbs_mpeg2_write_header(ctx, unit, &pbc);
315
316 if (err == AVERROR(ENOSPC)) {
317 // Overflow.
318 priv->write_buffer_size *= 2;
319 goto reallocate_and_try_again;
320 }
321 if (err < 0) {
322 // Write failed for some other reason.
323 return err;
324 }
325
326 if (put_bits_count(&pbc) % 8)
327 unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
328 else
329 unit->data_bit_padding = 0;
330
331 unit->data_size = (put_bits_count(&pbc) + 7) / 8;
332 flush_put_bits(&pbc);
333
334 err = av_reallocp(&unit->data, unit->data_size);
335 if (err < 0)
336 return err;
337
338 memcpy(unit->data, priv->write_buffer, unit->data_size);
339
340 return 0;
341}
342
343static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
344 CodedBitstreamFragment *frag)
345{
346 uint8_t *data;
347 size_t size, dp, sp;
348 int i;
349
350 size = 0;
351 for (i = 0; i < frag->nb_units; i++)
352 size += 3 + frag->units[i].data_size;
353
354 data = av_malloc(size);
355 if (!data)
356 return AVERROR(ENOMEM);
357
358 dp = 0;
359 for (i = 0; i < frag->nb_units; i++) {
360 CodedBitstreamUnit *unit = &frag->units[i];
361
362 data[dp++] = 0;
363 data[dp++] = 0;
364 data[dp++] = 1;
365
366 for (sp = 0; sp < unit->data_size; sp++)
367 data[dp++] = unit->data[sp];
368 }
369
370 av_assert0(dp == size);
371
372 frag->data = data;
373 frag->data_size = size;
374
375 return 0;
376}
377
378static void cbs_mpeg2_free_unit(CodedBitstreamUnit *unit)
379{
380 if (MPEG2_START_IS_SLICE(unit->type)) {
381 MPEG2RawSlice *slice = unit->content;
382 av_freep(&slice->data);
383 av_freep(&slice->header.extra_information);
384 } else if (unit->type == MPEG2_START_USER_DATA) {
385 MPEG2RawUserData *user = unit->content;
386 av_freep(&user->user_data);
387 }
388 av_freep(&unit->content);
389}
390
391static void cbs_mpeg2_close(CodedBitstreamContext *ctx)
392{
393 CodedBitstreamMPEG2Context *priv = ctx->priv_data;
394
395 av_freep(&priv->write_buffer);
396}
397
398const CodedBitstreamType ff_cbs_type_mpeg2 = {
399 .codec_id = AV_CODEC_ID_MPEG2VIDEO,
400
401 .priv_data_size = sizeof(CodedBitstreamMPEG2Context),
402
403 .split_fragment = &cbs_mpeg2_split_fragment,
404 .read_unit = &cbs_mpeg2_read_unit,
405 .write_unit = &cbs_mpeg2_write_unit,
406 .assemble_fragment = &cbs_mpeg2_assemble_fragment,
407
408 .free_unit = &cbs_mpeg2_free_unit,
409 .close = &cbs_mpeg2_close,
410};