1 | /*
|
---|
2 | * FLV encoder.
|
---|
3 | * Copyright (c) 2003 The FFmpeg Project.
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Lesser General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Lesser General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Lesser General Public
|
---|
16 | * License along with this library; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
18 | */
|
---|
19 | #include "avformat.h"
|
---|
20 |
|
---|
21 | static int flv_probe(AVProbeData *p)
|
---|
22 | {
|
---|
23 | const uint8_t *d;
|
---|
24 |
|
---|
25 | if (p->buf_size < 6)
|
---|
26 | return 0;
|
---|
27 | d = p->buf;
|
---|
28 | if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V') {
|
---|
29 | return 50;
|
---|
30 | }
|
---|
31 | return 0;
|
---|
32 | }
|
---|
33 |
|
---|
34 | static int flv_read_header(AVFormatContext *s,
|
---|
35 | AVFormatParameters *ap)
|
---|
36 | {
|
---|
37 | int offset, flags, size;
|
---|
38 |
|
---|
39 | s->ctx_flags |= AVFMTCTX_NOHEADER; //ok we have a header but theres no fps, codec type, sample_rate, ...
|
---|
40 |
|
---|
41 | url_fskip(&s->pb, 4);
|
---|
42 | flags = get_byte(&s->pb);
|
---|
43 |
|
---|
44 | offset = get_be32(&s->pb);
|
---|
45 |
|
---|
46 | if(!url_is_streamed(&s->pb)){
|
---|
47 | const int fsize= url_fsize(&s->pb);
|
---|
48 | url_fseek(&s->pb, fsize-4, SEEK_SET);
|
---|
49 | size= get_be32(&s->pb);
|
---|
50 | url_fseek(&s->pb, fsize-3-size, SEEK_SET);
|
---|
51 | if(size == get_be24(&s->pb) + 11){
|
---|
52 | s->duration= get_be24(&s->pb) * (int64_t)AV_TIME_BASE / 1000;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | url_fseek(&s->pb, offset, SEEK_SET);
|
---|
57 |
|
---|
58 | return 0;
|
---|
59 | }
|
---|
60 |
|
---|
61 | static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
|
---|
62 | {
|
---|
63 | int ret, i, type, size, pts, flags, is_audio, next;
|
---|
64 | AVStream *st = NULL;
|
---|
65 |
|
---|
66 | for(;;){
|
---|
67 | url_fskip(&s->pb, 4); /* size of previous packet */
|
---|
68 | type = get_byte(&s->pb);
|
---|
69 | size = get_be24(&s->pb);
|
---|
70 | pts = get_be24(&s->pb);
|
---|
71 | // av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, pts:%d\n", type, size, pts);
|
---|
72 | if (url_feof(&s->pb))
|
---|
73 | return AVERROR_IO;
|
---|
74 | url_fskip(&s->pb, 4); /* reserved */
|
---|
75 | flags = 0;
|
---|
76 |
|
---|
77 | if(size == 0)
|
---|
78 | continue;
|
---|
79 |
|
---|
80 | next= size + url_ftell(&s->pb);
|
---|
81 |
|
---|
82 | if (type == 8) {
|
---|
83 | is_audio=1;
|
---|
84 | flags = get_byte(&s->pb);
|
---|
85 | } else if (type == 9) {
|
---|
86 | is_audio=0;
|
---|
87 | flags = get_byte(&s->pb);
|
---|
88 | } else if (type == 18 && size > 13+1+4) {
|
---|
89 | url_fskip(&s->pb, 13); //onMetaData blah
|
---|
90 | if(get_byte(&s->pb) == 8){
|
---|
91 | url_fskip(&s->pb, 4);
|
---|
92 | }
|
---|
93 | while(url_ftell(&s->pb) + 5 < next){
|
---|
94 | char tmp[128];
|
---|
95 | int type, len;
|
---|
96 | double d= 0;
|
---|
97 |
|
---|
98 | len= get_be16(&s->pb);
|
---|
99 | if(len >= sizeof(tmp) || !len)
|
---|
100 | break;
|
---|
101 | get_buffer(&s->pb, tmp, len);
|
---|
102 | tmp[len]=0;
|
---|
103 |
|
---|
104 | type= get_byte(&s->pb);
|
---|
105 | if(type==0){
|
---|
106 | d= av_int2dbl(get_be64(&s->pb));
|
---|
107 | }else if(type==2){
|
---|
108 | len= get_be16(&s->pb);
|
---|
109 | if(len >= sizeof(tmp))
|
---|
110 | break;
|
---|
111 | url_fskip(&s->pb, len);
|
---|
112 | }else if(type==8){
|
---|
113 | //array
|
---|
114 | break;
|
---|
115 | }else if(type==11){
|
---|
116 | d= av_int2dbl(get_be64(&s->pb));
|
---|
117 | get_be16(&s->pb);
|
---|
118 | }
|
---|
119 |
|
---|
120 | if(!strcmp(tmp, "duration")){
|
---|
121 | s->duration = d*AV_TIME_BASE;
|
---|
122 | }else if(!strcmp(tmp, "videodatarate")){
|
---|
123 | }else if(!strcmp(tmp, "audiodatarate")){
|
---|
124 | }
|
---|
125 | }
|
---|
126 | url_fseek(&s->pb, next, SEEK_SET);
|
---|
127 | continue;
|
---|
128 | } else {
|
---|
129 | /* skip packet */
|
---|
130 | av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
|
---|
131 | url_fseek(&s->pb, next, SEEK_SET);
|
---|
132 | continue;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /* now find stream */
|
---|
136 | for(i=0;i<s->nb_streams;i++) {
|
---|
137 | st = s->streams[i];
|
---|
138 | if (st->id == is_audio)
|
---|
139 | break;
|
---|
140 | }
|
---|
141 | if(i == s->nb_streams){
|
---|
142 | st = av_new_stream(s, is_audio);
|
---|
143 | if (!st)
|
---|
144 | return AVERROR_NOMEM;
|
---|
145 |
|
---|
146 | av_set_pts_info(st, 24, 1, 1000); /* 24 bit pts in ms */
|
---|
147 | st->codec->time_base= (AVRational){1,1000};
|
---|
148 | }
|
---|
149 | // av_log(NULL, AV_LOG_DEBUG, "%d %X %d \n", is_audio, flags, st->discard);
|
---|
150 | if( (st->discard >= AVDISCARD_NONKEY && !((flags >> 4)==1 || is_audio))
|
---|
151 | ||(st->discard >= AVDISCARD_BIDIR && ((flags >> 4)==3 && !is_audio))
|
---|
152 | || st->discard >= AVDISCARD_ALL
|
---|
153 | ){
|
---|
154 | url_fseek(&s->pb, next, SEEK_SET);
|
---|
155 | continue;
|
---|
156 | }
|
---|
157 | break;
|
---|
158 | }
|
---|
159 |
|
---|
160 | if(is_audio){
|
---|
161 | if(st->codec->sample_rate == 0){
|
---|
162 | st->codec->codec_type = CODEC_TYPE_AUDIO;
|
---|
163 | st->codec->channels = (flags&1)+1;
|
---|
164 | if((flags >> 4) == 5)
|
---|
165 | st->codec->sample_rate= 8000;
|
---|
166 | else
|
---|
167 | st->codec->sample_rate = (44100<<((flags>>2)&3))>>3;
|
---|
168 | switch(flags >> 4){/* 0: uncompressed 1: ADPCM 2: mp3 5: Nellymoser 8kHz mono 6: Nellymoser*/
|
---|
169 | case 0: if (flags&2) st->codec->codec_id = CODEC_ID_PCM_S16BE;
|
---|
170 | else st->codec->codec_id = CODEC_ID_PCM_S8; break;
|
---|
171 | case 1: st->codec->codec_id = CODEC_ID_ADPCM_SWF; break;
|
---|
172 | case 2: st->codec->codec_id = CODEC_ID_MP3; break;
|
---|
173 | // this is not listed at FLV but at SWF, strange...
|
---|
174 | case 3: if (flags&2) st->codec->codec_id = CODEC_ID_PCM_S16LE;
|
---|
175 | else st->codec->codec_id = CODEC_ID_PCM_S8; break;
|
---|
176 | default:
|
---|
177 | av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flags >> 4);
|
---|
178 | st->codec->codec_tag= (flags >> 4);
|
---|
179 | }
|
---|
180 | st->codec->bits_per_sample = (flags & 2) ? 16 : 8;
|
---|
181 | }
|
---|
182 | }else{
|
---|
183 | st->codec->codec_type = CODEC_TYPE_VIDEO;
|
---|
184 | switch(flags & 0xF){
|
---|
185 | case 2: st->codec->codec_id = CODEC_ID_FLV1; break;
|
---|
186 | case 3: st->codec->codec_id = CODEC_ID_FLASHSV; break;
|
---|
187 | default:
|
---|
188 | av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flags & 0xf);
|
---|
189 | st->codec->codec_tag= flags & 0xF;
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | ret= av_get_packet(&s->pb, pkt, size - 1);
|
---|
194 | if (ret <= 0) {
|
---|
195 | return AVERROR_IO;
|
---|
196 | }
|
---|
197 | /* note: we need to modify the packet size here to handle the last
|
---|
198 | packet */
|
---|
199 | pkt->size = ret;
|
---|
200 | pkt->pts = pts;
|
---|
201 | pkt->stream_index = st->index;
|
---|
202 |
|
---|
203 | if (is_audio || ((flags >> 4)==1))
|
---|
204 | pkt->flags |= PKT_FLAG_KEY;
|
---|
205 |
|
---|
206 | return ret;
|
---|
207 | }
|
---|
208 |
|
---|
209 | static int flv_read_close(AVFormatContext *s)
|
---|
210 | {
|
---|
211 | return 0;
|
---|
212 | }
|
---|
213 |
|
---|
214 | AVInputFormat flv_demuxer = {
|
---|
215 | "flv",
|
---|
216 | "flv format",
|
---|
217 | 0,
|
---|
218 | flv_probe,
|
---|
219 | flv_read_header,
|
---|
220 | flv_read_packet,
|
---|
221 | flv_read_close,
|
---|
222 | .extensions = "flv",
|
---|
223 | .value = CODEC_ID_FLV1,
|
---|
224 | };
|
---|
225 |
|
---|
226 | int flvdec_init(void)
|
---|
227 | {
|
---|
228 | av_register_input_format(&flv_demuxer);
|
---|
229 | return 0;
|
---|
230 | }
|
---|