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