Attempt to fix the completely random values returned by ff_avc_find_startcode().
[libav.git] / 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 "avformat.h"
24 #include "avio.h"
25
26 static const uint8_t *ff_avc_find_startcode_internal(const uint8_t *p, const uint8_t *end)
27 {
28 const uint8_t *a = p + 4 - ((intptr_t)p & 3);
29
30 for (end -= 3; p < a && p < end; p++) {
31 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
32 return p;
33 }
34
35 for (end -= 3; p < end; p += 4) {
36 uint32_t x = *(const uint32_t*)p;
37 // if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
38 // if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
39 if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
40 if (p[1] == 0) {
41 if (p[0] == 0 && p[2] == 1)
42 return p;
43 if (p[2] == 0 && p[3] == 1)
44 return p+1;
45 }
46 if (p[3] == 0) {
47 if (p[2] == 0 && p[4] == 1)
48 return p+2;
49 if (p[4] == 0 && p[5] == 1)
50 return p+3;
51 }
52 }
53 }
54
55 for (end += 3; p < end; p++) {
56 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
57 return p;
58 }
59
60 return end + 3;
61 }
62
63 const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end){
64 const uint8_t *out= ff_avc_find_startcode_internal(p, end);
65 if(p<out && out<end && !out[-1]) out--;
66 return out;
67 }
68
69 int ff_avc_parse_nal_units(ByteIOContext *pb, const uint8_t *buf_in, int size)
70 {
71 const uint8_t *p = buf_in;
72 const uint8_t *end = p + size;
73 const uint8_t *nal_start, *nal_end;
74
75 size = 0;
76 nal_start = ff_avc_find_startcode(p, end);
77 while (nal_start < end) {
78 while(!*(nal_start++));
79 nal_end = ff_avc_find_startcode(nal_start, end);
80 put_be32(pb, nal_end - nal_start);
81 put_buffer(pb, nal_start, nal_end - nal_start);
82 size += 4 + nal_end - nal_start;
83 nal_start = nal_end;
84 }
85 return size;
86 }
87
88 int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
89 {
90 ByteIOContext *pb;
91 int ret = url_open_dyn_buf(&pb);
92 if(ret < 0)
93 return ret;
94
95 ff_avc_parse_nal_units(pb, buf_in, *size);
96
97 av_freep(buf);
98 *size = url_close_dyn_buf(pb, buf);
99 return 0;
100 }
101
102 int ff_isom_write_avcc(ByteIOContext *pb, const uint8_t *data, int len)
103 {
104 if (len > 6) {
105 /* check for h264 start code */
106 if (AV_RB32(data) == 0x00000001 ||
107 AV_RB24(data) == 0x000001) {
108 uint8_t *buf=NULL, *end, *start;
109 uint32_t sps_size=0, pps_size=0;
110 uint8_t *sps=0, *pps=0;
111
112 int ret = ff_avc_parse_nal_units_buf(data, &buf, &len);
113 if (ret < 0)
114 return ret;
115 start = buf;
116 end = buf + len;
117
118 /* look for sps and pps */
119 while (buf < end) {
120 unsigned int size;
121 uint8_t nal_type;
122 size = AV_RB32(buf);
123 nal_type = buf[4] & 0x1f;
124 if (nal_type == 7) { /* SPS */
125 sps = buf + 4;
126 sps_size = size;
127 } else if (nal_type == 8) { /* PPS */
128 pps = buf + 4;
129 pps_size = size;
130 }
131 buf += size + 4;
132 }
133 assert(sps);
134 assert(pps);
135
136 put_byte(pb, 1); /* version */
137 put_byte(pb, sps[1]); /* profile */
138 put_byte(pb, sps[2]); /* profile compat */
139 put_byte(pb, sps[3]); /* level */
140 put_byte(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
141 put_byte(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
142
143 put_be16(pb, sps_size);
144 put_buffer(pb, sps, sps_size);
145 put_byte(pb, 1); /* number of pps */
146 put_be16(pb, pps_size);
147 put_buffer(pb, pps, pps_size);
148 av_free(start);
149 } else {
150 put_buffer(pb, data, len);
151 }
152 }
153 return 0;
154 }