Rename var: val -> energy
[FFMpeg-mirror/DVCPRO-HD.git] / libavformat / avc.c
blob0c180c86b7820b90dbe9fba658efa9f4f604bf2d
1 /*
2 * AVC helper functions for muxers
3 * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
5 * This file is part of FFmpeg.
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.
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.
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
21 #include "avformat.h"
22 #include "avio.h"
24 const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end)
26 const uint8_t *a = p + 4 - ((long)p & 3);
28 for( end -= 3; p < a && p < end; p++ ) {
29 if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
30 return p;
33 for( end -= 3; p < end; p += 4 ) {
34 uint32_t x = *(const uint32_t*)p;
35 // if( (x - 0x01000100) & (~x) & 0x80008000 ) // little endian
36 // if( (x - 0x00010001) & (~x) & 0x00800080 ) // big endian
37 if( (x - 0x01010101) & (~x) & 0x80808080 ) { // generic
38 if( p[1] == 0 ) {
39 if( p[0] == 0 && p[2] == 1 )
40 return p-1;
41 if( p[2] == 0 && p[3] == 1 )
42 return p;
44 if( p[3] == 0 ) {
45 if( p[2] == 0 && p[4] == 1 )
46 return p+1;
47 if( p[4] == 0 && p[5] == 1 )
48 return p+2;
53 for( end += 3; p < end; p++ ) {
54 if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
55 return p;
58 return end + 3;
61 int ff_avc_parse_nal_units(const uint8_t *buf_in, uint8_t **buf, int *size)
63 ByteIOContext *pb;
64 const uint8_t *p = buf_in;
65 const uint8_t *end = p + *size;
66 const uint8_t *nal_start, *nal_end;
67 int ret = url_open_dyn_buf(&pb);
68 if(ret < 0)
69 return ret;
71 nal_start = ff_avc_find_startcode(p, end);
72 while (nal_start < end) {
73 while(!*(nal_start++));
74 nal_end = ff_avc_find_startcode(nal_start, end);
75 put_be32(pb, nal_end - nal_start);
76 put_buffer(pb, nal_start, nal_end - nal_start);
77 nal_start = nal_end;
79 av_freep(buf);
80 *size = url_close_dyn_buf(pb, buf);
81 return 0;
84 int ff_isom_write_avcc(ByteIOContext *pb, const uint8_t *data, int len)
86 if (len > 6) {
87 /* check for h264 start code */
88 if (AV_RB32(data) == 0x00000001) {
89 uint8_t *buf=NULL, *end, *start;
90 uint32_t sps_size=0, pps_size=0;
91 uint8_t *sps=0, *pps=0;
93 int ret = ff_avc_parse_nal_units(data, &buf, &len);
94 if (ret < 0)
95 return ret;
96 start = buf;
97 end = buf + len;
99 /* look for sps and pps */
100 while (buf < end) {
101 unsigned int size;
102 uint8_t nal_type;
103 size = AV_RB32(buf);
104 nal_type = buf[4] & 0x1f;
105 if (nal_type == 7) { /* SPS */
106 sps = buf + 4;
107 sps_size = size;
108 } else if (nal_type == 8) { /* PPS */
109 pps = buf + 4;
110 pps_size = size;
112 buf += size + 4;
114 assert(sps);
115 assert(pps);
117 put_byte(pb, 1); /* version */
118 put_byte(pb, sps[1]); /* profile */
119 put_byte(pb, sps[2]); /* profile compat */
120 put_byte(pb, sps[3]); /* level */
121 put_byte(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
122 put_byte(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
124 put_be16(pb, sps_size);
125 put_buffer(pb, sps, sps_size);
126 put_byte(pb, 1); /* number of pps */
127 put_be16(pb, pps_size);
128 put_buffer(pb, pps, pps_size);
129 av_free(start);
130 } else {
131 put_buffer(pb, data, len);
134 return 0;