FFmpeg
Loading...
Searching...
No Matches
cbs_mpeg2.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 "startcode.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#define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
42
43#define uir(width, name) \
44 xui(width, name, current->name, 1, MAX_UINT_BITS(width), 0, )
45#define uis(width, name, subs, ...) \
46 xui(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
47#define uirs(width, name, subs, ...) \
48 xui(width, name, current->name, 1, MAX_UINT_BITS(width), subs, __VA_ARGS__)
49#define xui(width, name, var, range_min, range_max, subs, ...) \
50 xuia(width, #name, var, range_min, range_max, subs, __VA_ARGS__)
51#define sis(width, name, subs, ...) \
52 xsi(width, name, current->name, subs, __VA_ARGS__)
53
54#define marker_bit() \
55 bit("marker_bit", 1)
56#define bit(string, value) do { \
57 av_unused uint32_t bit = value; \
58 xuia(1, string, bit, value, value, 0, ); \
59 } while (0)
60
61
62#define READ
63#define READWRITE read
64#define RWContext GetBitContext
65
66#define ui(width, name) do { \
67 uint32_t value; \
68 CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, #name, \
69 &value)); \
70 current->name = value; \
71 } while (0)
72#define xuia(width, string, var, range_min, range_max, subs, ...) do { \
73 uint32_t value; \
74 CHECK(ff_cbs_read_unsigned(ctx, rw, width, string, \
75 SUBSCRIPTS(subs, __VA_ARGS__), \
76 &value, range_min, range_max)); \
77 var = value; \
78 } while (0)
79
80#define xsi(width, name, var, subs, ...) do { \
81 int32_t value; \
82 CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
83 SUBSCRIPTS(subs, __VA_ARGS__), &value, \
84 MIN_INT_BITS(width), \
85 MAX_INT_BITS(width))); \
86 var = value; \
87 } while (0)
88
89#define nextbits(width, compare, var) \
90 (get_bits_left(rw) >= width && \
91 (var = show_bits(rw, width)) == (compare))
92
93#define infer(name, value) do { \
94 current->name = value; \
95 } while (0)
96
98
99#undef READ
100#undef READWRITE
101#undef RWContext
102#undef ui
103#undef xuia
104#undef xsi
105#undef nextbits
106#undef infer
107
108
109#define WRITE
110#define READWRITE write
111#define RWContext PutBitContext
112
113#define ui(width, name) do { \
114 CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
115 current->name)); \
116 } while (0)
117
118#define xuia(width, string, var, range_min, range_max, subs, ...) do { \
119 CHECK(ff_cbs_write_unsigned(ctx, rw, width, string, \
120 SUBSCRIPTS(subs, __VA_ARGS__), \
121 var, range_min, range_max)); \
122 } while (0)
123
124#define xsi(width, name, var, subs, ...) do { \
125 CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
126 SUBSCRIPTS(subs, __VA_ARGS__), var, \
127 MIN_INT_BITS(width), \
128 MAX_INT_BITS(width))); \
129 } while (0)
130
131#define nextbits(width, compare, var) (var)
132
133#define infer(name, value) do { \
134 if (current->name != (value)) { \
135 av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
136 "%s does not match inferred value: " \
137 "%"PRId64", but should be %"PRId64".\n", \
138 #name, (int64_t)current->name, (int64_t)(value)); \
139 } \
140 } while (0)
141
143
144#undef WRITE
145#undef READWRITE
146#undef RWContext
147#undef ui
148#undef xuia
149#undef xsi
150#undef nextbits
151#undef infer
152
153
156 int header)
157{
158 const uint8_t *start;
159 uint32_t start_code = -1;
160 int err;
161
162 start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
163 &start_code);
164 if (start_code >> 8 != 0x000001) {
165 // No start code found.
166 return AVERROR_INVALIDDATA;
167 }
168
169 do {
170 CodedBitstreamUnitType unit_type = start_code & 0xff;
171 const uint8_t *end;
172 size_t unit_size;
173
174 // Reset start_code to ensure that avpriv_find_start_code()
175 // really reads a new start code and does not reuse the old
176 // start code in any way (as e.g. happens when there is a
177 // Sequence End unit at the very end of a packet).
178 start_code = UINT32_MAX;
179 end = avpriv_find_start_code(start--, frag->data + frag->data_size,
180 &start_code);
181
182 // start points to the byte containing the start_code_identifier
183 // (may be the last byte of fragment->data); end points to the byte
184 // following the byte containing the start code identifier (or to
185 // the end of fragment->data).
186 if (start_code >> 8 == 0x000001) {
187 // Unit runs from start to the beginning of the start code
188 // pointed to by end (including any padding zeroes).
189 unit_size = (end - 4) - start;
190 } else {
191 // We didn't find a start code, so this is the final unit.
192 unit_size = end - start;
193 }
194
195 err = ff_cbs_append_unit_data(frag, unit_type, (uint8_t*)start,
196 unit_size, frag->data_ref);
197 if (err < 0)
198 return err;
199
200 start = end;
201
202 // Do we have a further unit to add to the fragment?
203 } while ((start_code >> 8) == 0x000001);
204
205 return 0;
206}
207
209 CodedBitstreamUnit *unit)
210{
211 GetBitContext gbc;
212 int err;
213
214 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
215 if (err < 0)
216 return err;
217
218 err = ff_cbs_alloc_unit_content(ctx, unit);
219 if (err < 0)
220 return err;
221
222 if (MPEG2_START_IS_SLICE(unit->type)) {
223 MPEG2RawSlice *slice = unit->content;
224 int pos, len;
225
226 err = cbs_mpeg2_read_slice_header(ctx, &gbc, &slice->header);
227 if (err < 0)
228 return err;
229
230 if (!get_bits_left(&gbc))
231 return AVERROR_INVALIDDATA;
232
233 pos = get_bits_count(&gbc);
234 len = unit->data_size;
235
236 slice->data_size = len - pos / 8;
237 slice->data_ref = av_buffer_ref(unit->data_ref);
238 if (!slice->data_ref)
239 return AVERROR(ENOMEM);
240 slice->data = unit->data + pos / 8;
241
242 slice->data_bit_start = pos % 8;
243
244 } else {
245 switch (unit->type) {
246#define START(start_code, type, read_func, free_func) \
247 case start_code: \
248 { \
249 type *header = unit->content; \
250 err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
251 if (err < 0) \
252 return err; \
253 } \
254 break;
256 picture_header, &cbs_mpeg2_free_picture_header);
258 user_data, &cbs_mpeg2_free_user_data);
267#undef START
268 default:
269 return AVERROR(ENOSYS);
270 }
271 }
272
273 return 0;
274}
275
277 CodedBitstreamUnit *unit,
278 PutBitContext *pbc)
279{
280 int err;
281
282 switch (unit->type) {
283#define START(start_code, type, func) \
284 case start_code: \
285 err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
286 break;
294#undef START
295 default:
296 av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
297 "code %02"PRIx32".\n", unit->type);
299 }
300
301 return err;
302}
303
305 CodedBitstreamUnit *unit,
306 PutBitContext *pbc)
307{
308 MPEG2RawSlice *slice = unit->content;
309 int err;
310
311 err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
312 if (err < 0)
313 return err;
314
315 if (slice->data) {
316 size_t rest = slice->data_size - (slice->data_bit_start + 7) / 8;
317 uint8_t *pos = slice->data + slice->data_bit_start / 8;
318
319 av_assert0(slice->data_bit_start >= 0 &&
320 slice->data_size > slice->data_bit_start / 8);
321
322 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
323 return AVERROR(ENOSPC);
324
325 // First copy the remaining bits of the first byte
326 if (slice->data_bit_start % 8)
327 put_bits(pbc, 8 - slice->data_bit_start % 8,
328 *pos++ & MAX_UINT_BITS(8 - slice->data_bit_start % 8));
329
330 if (put_bits_count(pbc) % 8 == 0) {
331 // If the writer is aligned at this point,
332 // memcpy can be used to improve performance.
333 // This is the normal case.
334 flush_put_bits(pbc);
335 memcpy(put_bits_ptr(pbc), pos, rest);
336 skip_put_bytes(pbc, rest);
337 } else {
338 // If not, we have to copy manually:
339 for (; rest > 3; rest -= 4, pos += 4)
340 put_bits32(pbc, AV_RB32(pos));
341
342 for (; rest; rest--, pos++)
343 put_bits(pbc, 8, *pos);
344
345 // Align with zeros
346 put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
347 }
348 }
349
350 return 0;
351}
352
354 CodedBitstreamUnit *unit,
355 PutBitContext *pbc)
356{
357 if (MPEG2_START_IS_SLICE(unit->type))
358 return cbs_mpeg2_write_slice (ctx, unit, pbc);
359 else
360 return cbs_mpeg2_write_header(ctx, unit, pbc);
361}
362
365{
366 uint8_t *data;
367 size_t size, dp;
368 int i;
369
370 size = 0;
371 for (i = 0; i < frag->nb_units; i++)
372 size += 3 + frag->units[i].data_size;
373
375 if (!frag->data_ref)
376 return AVERROR(ENOMEM);
377 data = frag->data_ref->data;
378
379 dp = 0;
380 for (i = 0; i < frag->nb_units; i++) {
381 CodedBitstreamUnit *unit = &frag->units[i];
382
383 data[dp++] = 0;
384 data[dp++] = 0;
385 data[dp++] = 1;
386
387 memcpy(data + dp, unit->data, unit->data_size);
388 dp += unit->data_size;
389 }
390
391 av_assert0(dp == size);
392
394 frag->data = data;
395 frag->data_size = size;
396
397 return 0;
398}
399
402 extra_information_picture.extra_information),
403
404 {
405 .nb_unit_types = CBS_UNIT_TYPE_RANGE,
406 .unit_type.range.start = 0x01,
407 .unit_type.range.end = 0xaf,
408
409 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
410 .content_size = sizeof(MPEG2RawSlice),
411 .type.ref = { .nb_offsets = 2,
412 .offsets = { offsetof(MPEG2RawSlice, header.extra_information_slice.extra_information),
413 offsetof(MPEG2RawSlice, data) } },
414 },
415
417 user_data),
418
423
425};
426
428 .codec_id = AV_CODEC_ID_MPEG2VIDEO,
429
430 .priv_data_size = sizeof(CodedBitstreamMPEG2Context),
431
432 .unit_types = cbs_mpeg2_unit_types,
433
434 .split_fragment = &cbs_mpeg2_split_fragment,
435 .read_unit = &cbs_mpeg2_read_unit,
436 .write_unit = &cbs_mpeg2_write_unit,
437 .assemble_fragment = &cbs_mpeg2_assemble_fragment,
438};
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int FUNC extension_data(CodedBitstreamContext *ctx, RWContext *rw, H265RawExtensionData *current)
static int FUNC picture_header(CodedBitstreamContext *ctx, RWContext *rw, H266RawPictureHeader *current)
#define CBS_UNIT_TYPE_POD(type_, structure)
@ CBS_UNIT_TYPE_RANGE
#define CBS_UNIT_TYPE_END_OF_LIST
#define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field)
@ CBS_CONTENT_TYPE_INTERNAL_REFS
#define MAX_UINT_BITS(length)
static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition cbs_mpeg2.c:363
const CodedBitstreamType ff_cbs_type_mpeg2
Definition cbs_mpeg2.c:427
#define START(start_code, type, read_func, free_func)
static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition cbs_mpeg2.c:304
static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition cbs_mpeg2.c:353
static CodedBitstreamUnitTypeDescriptor cbs_mpeg2_unit_types[]
Definition cbs_mpeg2.c:400
static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition cbs_mpeg2.c:276
static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition cbs_mpeg2.c:154
static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition cbs_mpeg2.c:208
#define MPEG2_START_IS_SLICE(type)
Definition cbs_mpeg2.h:40
@ MPEG2_START_PICTURE
Definition cbs_mpeg2.h:29
@ MPEG2_START_SEQUENCE_HEADER
Definition cbs_mpeg2.h:33
@ MPEG2_START_SEQUENCE_END
Definition cbs_mpeg2.h:36
@ MPEG2_START_EXTENSION
Definition cbs_mpeg2.h:35
@ MPEG2_START_GROUP
Definition cbs_mpeg2.h:37
@ MPEG2_START_USER_DATA
Definition cbs_mpeg2.h:32
static int FUNC user_data(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawUserData *current)
static int FUNC sequence_end(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceEnd *current)
static int FUNC group_of_pictures_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawGroupOfPicturesHeader *current)
static int FUNC sequence_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceHeader *current)
#define NULL
Definition coverity.c:32
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition codec_id.h:52
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition buffer.c:103
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition buffer.c:77
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
cl_device_type type
#define AV_RB32(p)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition cbs.h:66
const char data[16]
Definition mxf.c:149
static int put_bits_count(PutBitContext *s)
Definition put_bits.h:90
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition put_bits.h:402
static int put_bits_left(PutBitContext *s)
Definition put_bits.h:135
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
Definition put_bits.h:411
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition put_bits.h:301
static const uint8_t header[24]
Definition sdr2.c:68
unsigned int pos
Definition spdifenc.c:431
Accelerated start code search function for start codes common to MPEG-1/2/4 video,...
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
uint8_t * data
The data buffer.
Definition buffer.h:90
Context structure for coded bitstream operations.
Definition cbs.h:226
Coded bitstream fragment structure, combining one or more units.
Definition cbs.h:129
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition cbs.h:175
int nb_units
Number of units in this fragment.
Definition cbs.h:160
size_t data_size
The number of bytes in the bitstream.
Definition cbs.h:142
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition cbs.h:135
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition cbs.h:152
Coded bitstream unit structure.
Definition cbs.h:77
void * content
Pointer to the decomposed form of this unit.
Definition cbs.h:114
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition cbs.h:88
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition cbs.h:81
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition cbs.h:93
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition cbs.h:105
AVBufferRef * data_ref
Definition cbs_mpeg2.h:210
uint8_t * data
Definition cbs_mpeg2.h:209
MPEG2RawSliceHeader header
Definition cbs_mpeg2.h:207
size_t data_size
Definition cbs_mpeg2.h:211
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
int size
static const uint8_t start_code[]
int len