]> git.sesse.net Git - ffmpeg/blob - libavformat/avc.c
avformat/dump: Remove remnants of codec timebase
[ffmpeg] / libavformat / avc.c
1 /*
2  * AVC helper functions for muxers
3  * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.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 "libavutil/intreadwrite.h"
23 #include "libavcodec/h264.h"
24 #include "libavcodec/get_bits.h"
25 #include "avformat.h"
26 #include "avio.h"
27 #include "avc.h"
28 #include "avio_internal.h"
29
30 static const uint8_t *avc_find_startcode_internal(const uint8_t *p, const uint8_t *end)
31 {
32     const uint8_t *a = p + 4 - ((intptr_t)p & 3);
33
34     for (end -= 3; p < a && p < end; p++) {
35         if (p[0] == 0 && p[1] == 0 && p[2] == 1)
36             return p;
37     }
38
39     for (end -= 3; p < end; p += 4) {
40         uint32_t x = *(const uint32_t*)p;
41 //      if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
42 //      if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
43         if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
44             if (p[1] == 0) {
45                 if (p[0] == 0 && p[2] == 1)
46                     return p;
47                 if (p[2] == 0 && p[3] == 1)
48                     return p+1;
49             }
50             if (p[3] == 0) {
51                 if (p[2] == 0 && p[4] == 1)
52                     return p+2;
53                 if (p[4] == 0 && p[5] == 1)
54                     return p+3;
55             }
56         }
57     }
58
59     for (end += 3; p < end; p++) {
60         if (p[0] == 0 && p[1] == 0 && p[2] == 1)
61             return p;
62     }
63
64     return end + 3;
65 }
66
67 const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end){
68     const uint8_t *out = avc_find_startcode_internal(p, end);
69     if(p<out && out<end && !out[-1]) out--;
70     return out;
71 }
72
73 int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
74 {
75     const uint8_t *p = buf_in;
76     const uint8_t *end = p + size;
77     const uint8_t *nal_start, *nal_end;
78
79     size = 0;
80     nal_start = ff_avc_find_startcode(p, end);
81     for (;;) {
82         while (nal_start < end && !*(nal_start++));
83         if (nal_start == end)
84             break;
85
86         nal_end = ff_avc_find_startcode(nal_start, end);
87         avio_wb32(pb, nal_end - nal_start);
88         avio_write(pb, nal_start, nal_end - nal_start);
89         size += 4 + nal_end - nal_start;
90         nal_start = nal_end;
91     }
92     return size;
93 }
94
95 int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
96 {
97     AVIOContext *pb;
98     int ret = avio_open_dyn_buf(&pb);
99     if(ret < 0)
100         return ret;
101
102     ff_avc_parse_nal_units(pb, buf_in, *size);
103
104     *size = avio_close_dyn_buf(pb, buf);
105     return 0;
106 }
107
108 int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
109 {
110     AVIOContext *sps_pb = NULL, *pps_pb = NULL, *sps_ext_pb = NULL;
111     uint8_t *buf, *end, *start;
112     uint8_t *sps, *pps, *sps_ext;
113     uint32_t sps_size = 0, pps_size = 0, sps_ext_size = 0;
114     int ret, nb_sps = 0, nb_pps = 0, nb_sps_ext = 0;
115
116     if (len <= 6)
117         return AVERROR_INVALIDDATA;
118
119     /* check for H.264 start code */
120     if (AV_RB32(data) != 0x00000001 &&
121         AV_RB24(data) != 0x000001) {
122         avio_write(pb, data, len);
123         return 0;
124     }
125
126     ret = ff_avc_parse_nal_units_buf(data, &buf, &len);
127     if (ret < 0)
128         return ret;
129     start = buf;
130     end = buf + len;
131
132     ret = avio_open_dyn_buf(&sps_pb);
133     if (ret < 0)
134         goto fail;
135     ret = avio_open_dyn_buf(&pps_pb);
136     if (ret < 0)
137         goto fail;
138     ret = avio_open_dyn_buf(&sps_ext_pb);
139     if (ret < 0)
140         goto fail;
141
142     /* look for sps and pps */
143     while (end - buf > 4) {
144         uint32_t size;
145         uint8_t nal_type;
146         size = FFMIN(AV_RB32(buf), end - buf - 4);
147         buf += 4;
148         nal_type = buf[0] & 0x1f;
149
150         if (nal_type == 7) { /* SPS */
151             nb_sps++;
152             if (size > UINT16_MAX || nb_sps >= H264_MAX_SPS_COUNT) {
153                 ret = AVERROR_INVALIDDATA;
154                 goto fail;
155             }
156             avio_wb16(sps_pb, size);
157             avio_write(sps_pb, buf, size);
158         } else if (nal_type == 8) { /* PPS */
159             nb_pps++;
160             if (size > UINT16_MAX || nb_pps >= H264_MAX_PPS_COUNT) {
161                 ret = AVERROR_INVALIDDATA;
162                 goto fail;
163             }
164             avio_wb16(pps_pb, size);
165             avio_write(pps_pb, buf, size);
166         } else if (nal_type == 13) { /* SPS_EXT */
167             nb_sps_ext++;
168             if (size > UINT16_MAX || nb_sps_ext >= 256) {
169                 ret = AVERROR_INVALIDDATA;
170                 goto fail;
171             }
172             avio_wb16(sps_ext_pb, size);
173             avio_write(sps_ext_pb, buf, size);
174         }
175
176         buf += size;
177     }
178     sps_size = avio_get_dyn_buf(sps_pb, &sps);
179     pps_size = avio_get_dyn_buf(pps_pb, &pps);
180     sps_ext_size = avio_get_dyn_buf(sps_ext_pb, &sps_ext);
181
182     if (sps_size < 6 || !pps_size) {
183         ret = AVERROR_INVALIDDATA;
184         goto fail;
185     }
186
187     avio_w8(pb, 1); /* version */
188     avio_w8(pb, sps[3]); /* profile */
189     avio_w8(pb, sps[4]); /* profile compat */
190     avio_w8(pb, sps[5]); /* level */
191     avio_w8(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
192     avio_w8(pb, 0xe0 | nb_sps); /* 3 bits reserved (111) + 5 bits number of sps */
193
194     avio_write(pb, sps, sps_size);
195     avio_w8(pb, nb_pps); /* number of pps */
196     avio_write(pb, pps, pps_size);
197
198     if (sps[3] != 66 && sps[3] != 77 && sps[3] != 88) {
199         H264SPS seq;
200         ret = ff_avc_decode_sps(&seq, sps + 3, sps_size - 3);
201         if (ret < 0)
202             goto fail;
203
204         avio_w8(pb, 0xfc |  seq.chroma_format_idc); /* 6 bits reserved (111111) + chroma_format_idc */
205         avio_w8(pb, 0xf8 | (seq.bit_depth_luma - 8)); /* 5 bits reserved (11111) + bit_depth_luma_minus8 */
206         avio_w8(pb, 0xf8 | (seq.bit_depth_chroma - 8)); /* 5 bits reserved (11111) + bit_depth_chroma_minus8 */
207         avio_w8(pb, nb_sps_ext); /* number of sps ext */
208         if (nb_sps_ext)
209             avio_write(pb, sps_ext, sps_ext_size);
210     }
211
212 fail:
213     ffio_free_dyn_buf(&sps_pb);
214     ffio_free_dyn_buf(&pps_pb);
215     ffio_free_dyn_buf(&sps_ext_pb);
216     av_free(start);
217
218     return ret;
219 }
220
221 int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size)
222 {
223     uint16_t sps_size, pps_size;
224     uint8_t *out;
225     int out_size;
226
227     *buf = NULL;
228     if (*size >= 4 && (AV_RB32(in) == 0x00000001 || AV_RB24(in) == 0x000001))
229         return 0;
230     if (*size < 11 || in[0] != 1)
231         return AVERROR_INVALIDDATA;
232
233     sps_size = AV_RB16(&in[6]);
234     if (11 + sps_size > *size)
235         return AVERROR_INVALIDDATA;
236     pps_size = AV_RB16(&in[9 + sps_size]);
237     if (11 + sps_size + pps_size > *size)
238         return AVERROR_INVALIDDATA;
239     out_size = 8 + sps_size + pps_size;
240     out = av_mallocz(out_size + AV_INPUT_BUFFER_PADDING_SIZE);
241     if (!out)
242         return AVERROR(ENOMEM);
243     AV_WB32(&out[0], 0x00000001);
244     memcpy(out + 4, &in[8], sps_size);
245     AV_WB32(&out[4 + sps_size], 0x00000001);
246     memcpy(out + 8 + sps_size, &in[11 + sps_size], pps_size);
247     *buf = out;
248     *size = out_size;
249     return 0;
250 }
251
252 const uint8_t *ff_avc_mp4_find_startcode(const uint8_t *start,
253                                          const uint8_t *end,
254                                          int nal_length_size)
255 {
256     unsigned int res = 0;
257
258     if (end - start < nal_length_size)
259         return NULL;
260     while (nal_length_size--)
261         res = (res << 8) | *start++;
262
263     if (res > end - start)
264         return NULL;
265
266     return start + res;
267 }
268
269 uint8_t *ff_nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len,
270                                   uint32_t *dst_len, int header_len)
271 {
272     uint8_t *dst;
273     uint32_t i, len;
274
275     dst = av_malloc(src_len + AV_INPUT_BUFFER_PADDING_SIZE);
276     if (!dst)
277         return NULL;
278
279     /* NAL unit header */
280     i = len = 0;
281     while (i < header_len && i < src_len)
282         dst[len++] = src[i++];
283
284     while (i + 2 < src_len)
285         if (!src[i] && !src[i + 1] && src[i + 2] == 3) {
286             dst[len++] = src[i++];
287             dst[len++] = src[i++];
288             i++; // remove emulation_prevention_three_byte
289         } else
290             dst[len++] = src[i++];
291
292     while (i < src_len)
293         dst[len++] = src[i++];
294
295     memset(dst + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
296
297     *dst_len = len;
298     return dst;
299 }
300
301 static const AVRational avc_sample_aspect_ratio[17] = {
302     {   0,  1 },
303     {   1,  1 },
304     {  12, 11 },
305     {  10, 11 },
306     {  16, 11 },
307     {  40, 33 },
308     {  24, 11 },
309     {  20, 11 },
310     {  32, 11 },
311     {  80, 33 },
312     {  18, 11 },
313     {  15, 11 },
314     {  64, 33 },
315     { 160, 99 },
316     {   4,  3 },
317     {   3,  2 },
318     {   2,  1 },
319 };
320
321 static inline int get_ue_golomb(GetBitContext *gb) {
322     int i;
323     for (i = 0; i < 32 && !get_bits1(gb); i++)
324         ;
325     return get_bitsz(gb, i) + (1 << i) - 1;
326 }
327
328 static inline int get_se_golomb(GetBitContext *gb) {
329     int v = get_ue_golomb(gb) + 1;
330     int sign = -(v & 1);
331     return ((v >> 1) ^ sign) - sign;
332 }
333
334 int ff_avc_decode_sps(H264SPS *sps, const uint8_t *buf, int buf_size)
335 {
336     int i, j, ret, rbsp_size, aspect_ratio_idc, pic_order_cnt_type;
337     int num_ref_frames_in_pic_order_cnt_cycle;
338     int delta_scale, lastScale = 8, nextScale = 8;
339     int sizeOfScalingList;
340     GetBitContext gb;
341     uint8_t *rbsp_buf;
342
343     rbsp_buf = ff_nal_unit_extract_rbsp(buf, buf_size, &rbsp_size, 0);
344     if (!rbsp_buf)
345         return AVERROR(ENOMEM);
346
347     ret = init_get_bits8(&gb, rbsp_buf, rbsp_size);
348     if (ret < 0)
349         goto end;
350
351     memset(sps, 0, sizeof(*sps));
352
353     sps->profile_idc = get_bits(&gb, 8);
354     sps->constraint_set_flags |= get_bits1(&gb) << 0; // constraint_set0_flag
355     sps->constraint_set_flags |= get_bits1(&gb) << 1; // constraint_set1_flag
356     sps->constraint_set_flags |= get_bits1(&gb) << 2; // constraint_set2_flag
357     sps->constraint_set_flags |= get_bits1(&gb) << 3; // constraint_set3_flag
358     sps->constraint_set_flags |= get_bits1(&gb) << 4; // constraint_set4_flag
359     sps->constraint_set_flags |= get_bits1(&gb) << 5; // constraint_set5_flag
360     skip_bits(&gb, 2); // reserved_zero_2bits
361     sps->level_idc = get_bits(&gb, 8);
362     sps->id = get_ue_golomb(&gb);
363
364     if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
365         sps->profile_idc == 122 || sps->profile_idc == 244 || sps->profile_idc ==  44 ||
366         sps->profile_idc ==  83 || sps->profile_idc ==  86 || sps->profile_idc == 118 ||
367         sps->profile_idc == 128 || sps->profile_idc == 138 || sps->profile_idc == 139 ||
368         sps->profile_idc == 134) {
369         sps->chroma_format_idc = get_ue_golomb(&gb); // chroma_format_idc
370         if (sps->chroma_format_idc == 3) {
371             skip_bits1(&gb); // separate_colour_plane_flag
372         }
373         sps->bit_depth_luma = get_ue_golomb(&gb) + 8;
374         sps->bit_depth_chroma = get_ue_golomb(&gb) + 8;
375         skip_bits1(&gb); // qpprime_y_zero_transform_bypass_flag
376         if (get_bits1(&gb)) { // seq_scaling_matrix_present_flag
377             for (i = 0; i < ((sps->chroma_format_idc != 3) ? 8 : 12); i++) {
378                 if (!get_bits1(&gb)) // seq_scaling_list_present_flag
379                     continue;
380                 lastScale = 8;
381                 nextScale = 8;
382                 sizeOfScalingList = i < 6 ? 16 : 64;
383                 for (j = 0; j < sizeOfScalingList; j++) {
384                     if (nextScale != 0) {
385                         delta_scale = get_se_golomb(&gb);
386                         nextScale = (lastScale + delta_scale) & 0xff;
387                     }
388                     lastScale = nextScale == 0 ? lastScale : nextScale;
389                 }
390             }
391         }
392     } else {
393         sps->chroma_format_idc = 1;
394         sps->bit_depth_luma = 8;
395         sps->bit_depth_chroma = 8;
396     }
397
398     get_ue_golomb(&gb); // log2_max_frame_num_minus4
399     pic_order_cnt_type = get_ue_golomb(&gb);
400
401     if (pic_order_cnt_type == 0) {
402         get_ue_golomb(&gb); // log2_max_pic_order_cnt_lsb_minus4
403     } else if (pic_order_cnt_type == 1) {
404         skip_bits1(&gb);    // delta_pic_order_always_zero
405         get_se_golomb(&gb); // offset_for_non_ref_pic
406         get_se_golomb(&gb); // offset_for_top_to_bottom_field
407         num_ref_frames_in_pic_order_cnt_cycle = get_ue_golomb(&gb);
408         for (i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++)
409             get_se_golomb(&gb); // offset_for_ref_frame
410     }
411
412     get_ue_golomb(&gb); // max_num_ref_frames
413     skip_bits1(&gb); // gaps_in_frame_num_value_allowed_flag
414     get_ue_golomb(&gb); // pic_width_in_mbs_minus1
415     get_ue_golomb(&gb); // pic_height_in_map_units_minus1
416
417     sps->frame_mbs_only_flag = get_bits1(&gb);
418     if (!sps->frame_mbs_only_flag)
419         skip_bits1(&gb); // mb_adaptive_frame_field_flag
420
421     skip_bits1(&gb); // direct_8x8_inference_flag
422
423     if (get_bits1(&gb)) { // frame_cropping_flag
424         get_ue_golomb(&gb); // frame_crop_left_offset
425         get_ue_golomb(&gb); // frame_crop_right_offset
426         get_ue_golomb(&gb); // frame_crop_top_offset
427         get_ue_golomb(&gb); // frame_crop_bottom_offset
428     }
429
430     if (get_bits1(&gb)) { // vui_parameters_present_flag
431         if (get_bits1(&gb)) { // aspect_ratio_info_present_flag
432             aspect_ratio_idc = get_bits(&gb, 8);
433             if (aspect_ratio_idc == 0xff) {
434                 sps->sar.num = get_bits(&gb, 16);
435                 sps->sar.den = get_bits(&gb, 16);
436             } else if (aspect_ratio_idc < FF_ARRAY_ELEMS(avc_sample_aspect_ratio)) {
437                 sps->sar = avc_sample_aspect_ratio[aspect_ratio_idc];
438             }
439         }
440     }
441
442     if (!sps->sar.den) {
443         sps->sar.num = 1;
444         sps->sar.den = 1;
445     }
446
447     ret = 0;
448  end:
449     av_free(rbsp_buf);
450     return ret;
451 }