Mercurial > libavformat.hg
annotate avc.c @ 5720:d833557e7287 libavformat
asfenc: write tags in proper UTF-16.
Patch by Anton Khirnov wyskas gmail com
author | benoit |
---|---|
date | Thu, 25 Feb 2010 16:59:55 +0000 |
parents | a84525174bcf |
children | d63669d827bc |
rev | line source |
---|---|
201
85def00971c3
mov, 3gp, mp4 muxer by (Thomas Raivio <tjraivio at cc dot hut dot fi>)
michaelni
parents:
diff
changeset
|
1 /* |
2922
dcd599833abc
Move isom_write_avcc() and related functions into a separate file.
aurel
parents:
2921
diff
changeset
|
2 * AVC helper functions for muxers |
dcd599833abc
Move isom_write_avcc() and related functions into a separate file.
aurel
parents:
2921
diff
changeset
|
3 * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com> |
201
85def00971c3
mov, 3gp, mp4 muxer by (Thomas Raivio <tjraivio at cc dot hut dot fi>)
michaelni
parents:
diff
changeset
|
4 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1292
diff
changeset
|
5 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1292
diff
changeset
|
6 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1292
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
201
85def00971c3
mov, 3gp, mp4 muxer by (Thomas Raivio <tjraivio at cc dot hut dot fi>)
michaelni
parents:
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public |
85def00971c3
mov, 3gp, mp4 muxer by (Thomas Raivio <tjraivio at cc dot hut dot fi>)
michaelni
parents:
diff
changeset
|
9 * License as published by the Free Software Foundation; either |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1292
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
201
85def00971c3
mov, 3gp, mp4 muxer by (Thomas Raivio <tjraivio at cc dot hut dot fi>)
michaelni
parents:
diff
changeset
|
11 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1292
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
201
85def00971c3
mov, 3gp, mp4 muxer by (Thomas Raivio <tjraivio at cc dot hut dot fi>)
michaelni
parents:
diff
changeset
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
85def00971c3
mov, 3gp, mp4 muxer by (Thomas Raivio <tjraivio at cc dot hut dot fi>)
michaelni
parents:
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
85def00971c3
mov, 3gp, mp4 muxer by (Thomas Raivio <tjraivio at cc dot hut dot fi>)
michaelni
parents:
diff
changeset
|
15 * Lesser General Public License for more details. |
85def00971c3
mov, 3gp, mp4 muxer by (Thomas Raivio <tjraivio at cc dot hut dot fi>)
michaelni
parents:
diff
changeset
|
16 * |
85def00971c3
mov, 3gp, mp4 muxer by (Thomas Raivio <tjraivio at cc dot hut dot fi>)
michaelni
parents:
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1292
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
890
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
201
85def00971c3
mov, 3gp, mp4 muxer by (Thomas Raivio <tjraivio at cc dot hut dot fi>)
michaelni
parents:
diff
changeset
|
20 */ |
4201
7d2f3f1b68d8
Fix build: Add intreadwrite.h and bswap.h #includes where necessary.
diego
parents:
3050
diff
changeset
|
21 |
7d2f3f1b68d8
Fix build: Add intreadwrite.h and bswap.h #includes where necessary.
diego
parents:
3050
diff
changeset
|
22 #include "libavutil/intreadwrite.h" |
201
85def00971c3
mov, 3gp, mp4 muxer by (Thomas Raivio <tjraivio at cc dot hut dot fi>)
michaelni
parents:
diff
changeset
|
23 #include "avformat.h" |
85def00971c3
mov, 3gp, mp4 muxer by (Thomas Raivio <tjraivio at cc dot hut dot fi>)
michaelni
parents:
diff
changeset
|
24 #include "avio.h" |
85def00971c3
mov, 3gp, mp4 muxer by (Thomas Raivio <tjraivio at cc dot hut dot fi>)
michaelni
parents:
diff
changeset
|
25 |
5686
a84525174bcf
Attempt to fix the completely random values returned by ff_avc_find_startcode().
michael
parents:
5122
diff
changeset
|
26 static const uint8_t *ff_avc_find_startcode_internal(const uint8_t *p, const uint8_t *end) |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
27 { |
4792 | 28 const uint8_t *a = p + 4 - ((intptr_t)p & 3); |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
29 |
5122
6ea4f08de89a
cosmetics, remove spaces after '(' and before ')'
bcoudurier
parents:
4792
diff
changeset
|
30 for (end -= 3; p < a && p < end; p++) { |
6ea4f08de89a
cosmetics, remove spaces after '(' and before ')'
bcoudurier
parents:
4792
diff
changeset
|
31 if (p[0] == 0 && p[1] == 0 && p[2] == 1) |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
32 return p; |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
33 } |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
34 |
5122
6ea4f08de89a
cosmetics, remove spaces after '(' and before ')'
bcoudurier
parents:
4792
diff
changeset
|
35 for (end -= 3; p < end; p += 4) { |
3050
be4d4abb3eed
Fix misplaced const, avoids a cast-discards-qualifiers warning
reimar
parents:
3049
diff
changeset
|
36 uint32_t x = *(const uint32_t*)p; |
5122
6ea4f08de89a
cosmetics, remove spaces after '(' and before ')'
bcoudurier
parents:
4792
diff
changeset
|
37 // if ((x - 0x01000100) & (~x) & 0x80008000) // little endian |
6ea4f08de89a
cosmetics, remove spaces after '(' and before ')'
bcoudurier
parents:
4792
diff
changeset
|
38 // if ((x - 0x00010001) & (~x) & 0x00800080) // big endian |
6ea4f08de89a
cosmetics, remove spaces after '(' and before ')'
bcoudurier
parents:
4792
diff
changeset
|
39 if ((x - 0x01010101) & (~x) & 0x80808080) { // generic |
6ea4f08de89a
cosmetics, remove spaces after '(' and before ')'
bcoudurier
parents:
4792
diff
changeset
|
40 if (p[1] == 0) { |
6ea4f08de89a
cosmetics, remove spaces after '(' and before ')'
bcoudurier
parents:
4792
diff
changeset
|
41 if (p[0] == 0 && p[2] == 1) |
5686
a84525174bcf
Attempt to fix the completely random values returned by ff_avc_find_startcode().
michael
parents:
5122
diff
changeset
|
42 return p; |
5122
6ea4f08de89a
cosmetics, remove spaces after '(' and before ')'
bcoudurier
parents:
4792
diff
changeset
|
43 if (p[2] == 0 && p[3] == 1) |
5686
a84525174bcf
Attempt to fix the completely random values returned by ff_avc_find_startcode().
michael
parents:
5122
diff
changeset
|
44 return p+1; |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
45 } |
5122
6ea4f08de89a
cosmetics, remove spaces after '(' and before ')'
bcoudurier
parents:
4792
diff
changeset
|
46 if (p[3] == 0) { |
6ea4f08de89a
cosmetics, remove spaces after '(' and before ')'
bcoudurier
parents:
4792
diff
changeset
|
47 if (p[2] == 0 && p[4] == 1) |
5686
a84525174bcf
Attempt to fix the completely random values returned by ff_avc_find_startcode().
michael
parents:
5122
diff
changeset
|
48 return p+2; |
5122
6ea4f08de89a
cosmetics, remove spaces after '(' and before ')'
bcoudurier
parents:
4792
diff
changeset
|
49 if (p[4] == 0 && p[5] == 1) |
5686
a84525174bcf
Attempt to fix the completely random values returned by ff_avc_find_startcode().
michael
parents:
5122
diff
changeset
|
50 return p+3; |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
51 } |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
52 } |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
53 } |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
54 |
5122
6ea4f08de89a
cosmetics, remove spaces after '(' and before ')'
bcoudurier
parents:
4792
diff
changeset
|
55 for (end += 3; p < end; p++) { |
6ea4f08de89a
cosmetics, remove spaces after '(' and before ')'
bcoudurier
parents:
4792
diff
changeset
|
56 if (p[0] == 0 && p[1] == 0 && p[2] == 1) |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
57 return p; |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
58 } |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
59 |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
60 return end + 3; |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
61 } |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
62 |
5686
a84525174bcf
Attempt to fix the completely random values returned by ff_avc_find_startcode().
michael
parents:
5122
diff
changeset
|
63 const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end){ |
a84525174bcf
Attempt to fix the completely random values returned by ff_avc_find_startcode().
michael
parents:
5122
diff
changeset
|
64 const uint8_t *out= ff_avc_find_startcode_internal(p, end); |
a84525174bcf
Attempt to fix the completely random values returned by ff_avc_find_startcode().
michael
parents:
5122
diff
changeset
|
65 if(p<out && out<end && !out[-1]) out--; |
a84525174bcf
Attempt to fix the completely random values returned by ff_avc_find_startcode().
michael
parents:
5122
diff
changeset
|
66 return out; |
a84525174bcf
Attempt to fix the completely random values returned by ff_avc_find_startcode().
michael
parents:
5122
diff
changeset
|
67 } |
a84525174bcf
Attempt to fix the completely random values returned by ff_avc_find_startcode().
michael
parents:
5122
diff
changeset
|
68 |
4224 | 69 int ff_avc_parse_nal_units(ByteIOContext *pb, const uint8_t *buf_in, int size) |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
70 { |
3049 | 71 const uint8_t *p = buf_in; |
4222
0267c4299992
Do not reallocate AVPacket's data when muxing a packet
lucabe
parents:
4201
diff
changeset
|
72 const uint8_t *end = p + size; |
3049 | 73 const uint8_t *nal_start, *nal_end; |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
74 |
4224 | 75 size = 0; |
2935
88a67636f192
Make avc_find_startcode non-static, so that it can be used from other
lucabe
parents:
2927
diff
changeset
|
76 nal_start = ff_avc_find_startcode(p, end); |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
77 while (nal_start < end) { |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
78 while(!*(nal_start++)); |
2935
88a67636f192
Make avc_find_startcode non-static, so that it can be used from other
lucabe
parents:
2927
diff
changeset
|
79 nal_end = ff_avc_find_startcode(nal_start, end); |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2669
diff
changeset
|
80 put_be32(pb, nal_end - nal_start); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2669
diff
changeset
|
81 put_buffer(pb, nal_start, nal_end - nal_start); |
4224 | 82 size += 4 + nal_end - nal_start; |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
83 nal_start = nal_end; |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
84 } |
4224 | 85 return size; |
4222
0267c4299992
Do not reallocate AVPacket's data when muxing a packet
lucabe
parents:
4201
diff
changeset
|
86 } |
0267c4299992
Do not reallocate AVPacket's data when muxing a packet
lucabe
parents:
4201
diff
changeset
|
87 |
4226 | 88 int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size) |
4222
0267c4299992
Do not reallocate AVPacket's data when muxing a packet
lucabe
parents:
4201
diff
changeset
|
89 { |
0267c4299992
Do not reallocate AVPacket's data when muxing a packet
lucabe
parents:
4201
diff
changeset
|
90 ByteIOContext *pb; |
0267c4299992
Do not reallocate AVPacket's data when muxing a packet
lucabe
parents:
4201
diff
changeset
|
91 int ret = url_open_dyn_buf(&pb); |
0267c4299992
Do not reallocate AVPacket's data when muxing a packet
lucabe
parents:
4201
diff
changeset
|
92 if(ret < 0) |
0267c4299992
Do not reallocate AVPacket's data when muxing a packet
lucabe
parents:
4201
diff
changeset
|
93 return ret; |
0267c4299992
Do not reallocate AVPacket's data when muxing a packet
lucabe
parents:
4201
diff
changeset
|
94 |
0267c4299992
Do not reallocate AVPacket's data when muxing a packet
lucabe
parents:
4201
diff
changeset
|
95 ff_avc_parse_nal_units(pb, buf_in, *size); |
0267c4299992
Do not reallocate AVPacket's data when muxing a packet
lucabe
parents:
4201
diff
changeset
|
96 |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
97 av_freep(buf); |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2669
diff
changeset
|
98 *size = url_close_dyn_buf(pb, buf); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2669
diff
changeset
|
99 return 0; |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
100 } |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
101 |
3049 | 102 int ff_isom_write_avcc(ByteIOContext *pb, const uint8_t *data, int len) |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
103 { |
2921 | 104 if (len > 6) { |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
105 /* check for h264 start code */ |
4229 | 106 if (AV_RB32(data) == 0x00000001 || |
107 AV_RB24(data) == 0x000001) { | |
3049 | 108 uint8_t *buf=NULL, *end, *start; |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
109 uint32_t sps_size=0, pps_size=0; |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
110 uint8_t *sps=0, *pps=0; |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
111 |
4222
0267c4299992
Do not reallocate AVPacket's data when muxing a packet
lucabe
parents:
4201
diff
changeset
|
112 int ret = ff_avc_parse_nal_units_buf(data, &buf, &len); |
2919 | 113 if (ret < 0) |
114 return ret; | |
3049 | 115 start = buf; |
2921 | 116 end = buf + len; |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
117 |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
118 /* look for sps and pps */ |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
119 while (buf < end) { |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
120 unsigned int size; |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
121 uint8_t nal_type; |
1673 | 122 size = AV_RB32(buf); |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
123 nal_type = buf[4] & 0x1f; |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
124 if (nal_type == 7) { /* SPS */ |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
125 sps = buf + 4; |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
126 sps_size = size; |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
127 } else if (nal_type == 8) { /* PPS */ |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
128 pps = buf + 4; |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
129 pps_size = size; |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
130 } |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
131 buf += size + 4; |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
132 } |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
133 assert(sps); |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
134 assert(pps); |
1056
4af4c74a4548
get avcC profile and level indication from sps (from handbrake)
bcoudurier
parents:
1055
diff
changeset
|
135 |
4af4c74a4548
get avcC profile and level indication from sps (from handbrake)
bcoudurier
parents:
1055
diff
changeset
|
136 put_byte(pb, 1); /* version */ |
4af4c74a4548
get avcC profile and level indication from sps (from handbrake)
bcoudurier
parents:
1055
diff
changeset
|
137 put_byte(pb, sps[1]); /* profile */ |
4af4c74a4548
get avcC profile and level indication from sps (from handbrake)
bcoudurier
parents:
1055
diff
changeset
|
138 put_byte(pb, sps[2]); /* profile compat */ |
4af4c74a4548
get avcC profile and level indication from sps (from handbrake)
bcoudurier
parents:
1055
diff
changeset
|
139 put_byte(pb, sps[3]); /* level */ |
4af4c74a4548
get avcC profile and level indication from sps (from handbrake)
bcoudurier
parents:
1055
diff
changeset
|
140 put_byte(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */ |
4af4c74a4548
get avcC profile and level indication from sps (from handbrake)
bcoudurier
parents:
1055
diff
changeset
|
141 put_byte(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */ |
4af4c74a4548
get avcC profile and level indication from sps (from handbrake)
bcoudurier
parents:
1055
diff
changeset
|
142 |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
143 put_be16(pb, sps_size); |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
144 put_buffer(pb, sps, sps_size); |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
145 put_byte(pb, 1); /* number of pps */ |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
146 put_be16(pb, pps_size); |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
147 put_buffer(pb, pps, pps_size); |
3049 | 148 av_free(start); |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
149 } else { |
2921 | 150 put_buffer(pb, data, len); |
1006
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
151 } |
15bfc3c063dc
do nal reformating when muxing H264, create avcc atom
bcoudurier
parents:
1000
diff
changeset
|
152 } |
2921 | 153 return 0; |
154 } |