00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "libavutil/avstring.h"
00028 #include "libavutil/dict.h"
00029 #include "libavcodec/bytestream.h"
00030 #include "libavcodec/mpeg4audio.h"
00031 #include "avformat.h"
00032 #include "avio_internal.h"
00033 #include "flv.h"
00034
00035 typedef struct {
00036 int wrong_dts;
00037 } FLVContext;
00038
00039 static int flv_probe(AVProbeData *p)
00040 {
00041 const uint8_t *d;
00042
00043 d = p->buf;
00044 if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
00045 return AVPROBE_SCORE_MAX;
00046 }
00047 return 0;
00048 }
00049
00050 static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, int flv_codecid) {
00051 AVCodecContext *acodec = astream->codec;
00052 switch(flv_codecid) {
00053
00054 case FLV_CODECID_PCM:
00055 acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 :
00056 #if HAVE_BIGENDIAN
00057 CODEC_ID_PCM_S16BE;
00058 #else
00059 CODEC_ID_PCM_S16LE;
00060 #endif
00061 break;
00062 case FLV_CODECID_PCM_LE:
00063 acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 : CODEC_ID_PCM_S16LE; break;
00064 case FLV_CODECID_AAC : acodec->codec_id = CODEC_ID_AAC; break;
00065 case FLV_CODECID_ADPCM: acodec->codec_id = CODEC_ID_ADPCM_SWF; break;
00066 case FLV_CODECID_SPEEX:
00067 acodec->codec_id = CODEC_ID_SPEEX;
00068 acodec->sample_rate = 16000;
00069 break;
00070 case FLV_CODECID_MP3 : acodec->codec_id = CODEC_ID_MP3 ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
00071 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
00072 acodec->sample_rate = 8000;
00073 acodec->codec_id = CODEC_ID_NELLYMOSER;
00074 break;
00075 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
00076 acodec->sample_rate = 16000;
00077 acodec->codec_id = CODEC_ID_NELLYMOSER;
00078 break;
00079 case FLV_CODECID_NELLYMOSER:
00080 acodec->codec_id = CODEC_ID_NELLYMOSER;
00081 break;
00082 default:
00083 av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
00084 acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
00085 }
00086 }
00087
00088 static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
00089 AVCodecContext *vcodec = vstream->codec;
00090 switch(flv_codecid) {
00091 case FLV_CODECID_H263 : vcodec->codec_id = CODEC_ID_FLV1 ; break;
00092 case FLV_CODECID_SCREEN: vcodec->codec_id = CODEC_ID_FLASHSV; break;
00093 case FLV_CODECID_SCREEN2: vcodec->codec_id = CODEC_ID_FLASHSV2; break;
00094 case FLV_CODECID_VP6 : vcodec->codec_id = CODEC_ID_VP6F ;
00095 case FLV_CODECID_VP6A :
00096 if(flv_codecid == FLV_CODECID_VP6A)
00097 vcodec->codec_id = CODEC_ID_VP6A;
00098 if(vcodec->extradata_size != 1) {
00099 vcodec->extradata_size = 1;
00100 vcodec->extradata = av_malloc(1);
00101 }
00102 vcodec->extradata[0] = avio_r8(s->pb);
00103 return 1;
00104 case FLV_CODECID_H264:
00105 vcodec->codec_id = CODEC_ID_H264;
00106 return 3;
00107 default:
00108 av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
00109 vcodec->codec_tag = flv_codecid;
00110 }
00111
00112 return 0;
00113 }
00114
00115 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize) {
00116 int length = avio_rb16(ioc);
00117 if(length >= buffsize) {
00118 avio_skip(ioc, length);
00119 return -1;
00120 }
00121
00122 avio_read(ioc, buffer, length);
00123
00124 buffer[length] = '\0';
00125
00126 return length;
00127 }
00128
00129 static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, AVStream *vstream, int64_t max_pos) {
00130 unsigned int timeslen = 0, fileposlen = 0, i;
00131 char str_val[256];
00132 int64_t *times = NULL;
00133 int64_t *filepositions = NULL;
00134 int ret = AVERROR(ENOSYS);
00135 int64_t initial_pos = avio_tell(ioc);
00136
00137 while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
00138 int64_t** current_array;
00139 unsigned int arraylen;
00140
00141
00142 if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
00143 break;
00144
00145 arraylen = avio_rb32(ioc);
00146 if(arraylen>>28)
00147 break;
00148
00149 if (!strcmp(KEYFRAMES_TIMESTAMP_TAG , str_val) && !times){
00150 current_array= ×
00151 timeslen= arraylen;
00152 }else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) && !filepositions){
00153 current_array= &filepositions;
00154 fileposlen= arraylen;
00155 }else
00156 break;
00157
00158 if (!(*current_array = av_mallocz(sizeof(**current_array) * arraylen))) {
00159 ret = AVERROR(ENOMEM);
00160 goto finish;
00161 }
00162
00163 for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
00164 if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
00165 goto finish;
00166 current_array[0][i] = av_int2dbl(avio_rb64(ioc));
00167 }
00168 if (times && filepositions) {
00169
00170
00171 ret = 0;
00172 break;
00173 }
00174 }
00175
00176 if (!ret && timeslen == fileposlen) {
00177 for (i = 0; i < fileposlen; i++)
00178 av_add_index_entry(vstream, filepositions[i], times[i]*1000, 0, 0, AVINDEX_KEYFRAME);
00179 } else
00180 av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
00181
00182 finish:
00183 av_freep(×);
00184 av_freep(&filepositions);
00185 avio_seek(ioc, initial_pos, SEEK_SET);
00186 return ret;
00187 }
00188
00189 static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
00190 AVCodecContext *acodec, *vcodec;
00191 AVIOContext *ioc;
00192 AMFDataType amf_type;
00193 char str_val[256];
00194 double num_val;
00195
00196 num_val = 0;
00197 ioc = s->pb;
00198
00199 amf_type = avio_r8(ioc);
00200
00201 switch(amf_type) {
00202 case AMF_DATA_TYPE_NUMBER:
00203 num_val = av_int2dbl(avio_rb64(ioc)); break;
00204 case AMF_DATA_TYPE_BOOL:
00205 num_val = avio_r8(ioc); break;
00206 case AMF_DATA_TYPE_STRING:
00207 if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
00208 return -1;
00209 break;
00210 case AMF_DATA_TYPE_OBJECT: {
00211 unsigned int keylen;
00212
00213 if (vstream && ioc->seekable && key && !strcmp(KEYFRAMES_TAG, key) && depth == 1)
00214 if (parse_keyframes_index(s, ioc, vstream, max_pos) < 0)
00215 av_log(s, AV_LOG_ERROR, "Keyframe index parsing failed\n");
00216
00217 while(avio_tell(ioc) < max_pos - 2 && (keylen = avio_rb16(ioc))) {
00218 avio_skip(ioc, keylen);
00219 if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
00220 return -1;
00221 }
00222 if(avio_r8(ioc) != AMF_END_OF_OBJECT)
00223 return -1;
00224 }
00225 break;
00226 case AMF_DATA_TYPE_NULL:
00227 case AMF_DATA_TYPE_UNDEFINED:
00228 case AMF_DATA_TYPE_UNSUPPORTED:
00229 break;
00230 case AMF_DATA_TYPE_MIXEDARRAY:
00231 avio_skip(ioc, 4);
00232 while(avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
00233
00234 if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
00235 return -1;
00236 }
00237 if(avio_r8(ioc) != AMF_END_OF_OBJECT)
00238 return -1;
00239 break;
00240 case AMF_DATA_TYPE_ARRAY: {
00241 unsigned int arraylen, i;
00242
00243 arraylen = avio_rb32(ioc);
00244 for(i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
00245 if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
00246 return -1;
00247 }
00248 }
00249 break;
00250 case AMF_DATA_TYPE_DATE:
00251 avio_skip(ioc, 8 + 2);
00252 break;
00253 default:
00254 return -1;
00255 }
00256
00257 if(depth == 1 && key) {
00258 acodec = astream ? astream->codec : NULL;
00259 vcodec = vstream ? vstream->codec : NULL;
00260
00261 if(amf_type == AMF_DATA_TYPE_BOOL) {
00262 av_strlcpy(str_val, num_val > 0 ? "true" : "false", sizeof(str_val));
00263 av_dict_set(&s->metadata, key, str_val, 0);
00264 } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
00265 snprintf(str_val, sizeof(str_val), "%.f", num_val);
00266 av_dict_set(&s->metadata, key, str_val, 0);
00267 if(!strcmp(key, "duration")) s->duration = num_val * AV_TIME_BASE;
00268 else if(!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
00269 vcodec->bit_rate = num_val * 1024.0;
00270 else if(!strcmp(key, "audiodatarate") && acodec && 0 <= (int)(num_val * 1024.0))
00271 acodec->bit_rate = num_val * 1024.0;
00272 } else if(amf_type == AMF_DATA_TYPE_OBJECT){
00273 if(s->nb_streams==1 && ((!acodec && !strcmp(key, "audiocodecid")) || (!vcodec && !strcmp(key, "videocodecid")))){
00274 s->ctx_flags &= ~AVFMTCTX_NOHEADER;
00275 }
00276 } else if (amf_type == AMF_DATA_TYPE_STRING)
00277 av_dict_set(&s->metadata, key, str_val, 0);
00278 }
00279
00280 return 0;
00281 }
00282
00283 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
00284 AMFDataType type;
00285 AVStream *stream, *astream, *vstream;
00286 AVIOContext *ioc;
00287 int i;
00288 char buffer[11];
00289
00290 astream = NULL;
00291 vstream = NULL;
00292 ioc = s->pb;
00293
00294
00295 type = avio_r8(ioc);
00296 if(type != AMF_DATA_TYPE_STRING || amf_get_string(ioc, buffer, sizeof(buffer)) < 0 || strcmp(buffer, "onMetaData"))
00297 return -1;
00298
00299
00300 for(i = 0; i < s->nb_streams; i++) {
00301 stream = s->streams[i];
00302 if (stream->codec->codec_type == AVMEDIA_TYPE_AUDIO) astream = stream;
00303 else if(stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) vstream = stream;
00304 }
00305
00306
00307 if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
00308 return -1;
00309
00310 return 0;
00311 }
00312
00313 static AVStream *create_stream(AVFormatContext *s, int is_audio){
00314 AVStream *st = av_new_stream(s, is_audio);
00315 if (!st)
00316 return NULL;
00317 st->codec->codec_type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
00318 av_set_pts_info(st, 32, 1, 1000);
00319 return st;
00320 }
00321
00322 static int flv_read_header(AVFormatContext *s,
00323 AVFormatParameters *ap)
00324 {
00325 int offset, flags;
00326
00327 avio_skip(s->pb, 4);
00328 flags = avio_r8(s->pb);
00329
00330
00331 if (!flags) {
00332 flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
00333 av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
00334 }
00335
00336 if((flags & (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
00337 != (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
00338 s->ctx_flags |= AVFMTCTX_NOHEADER;
00339
00340 if(flags & FLV_HEADER_FLAG_HASVIDEO){
00341 if(!create_stream(s, 0))
00342 return AVERROR(ENOMEM);
00343 }
00344 if(flags & FLV_HEADER_FLAG_HASAUDIO){
00345 if(!create_stream(s, 1))
00346 return AVERROR(ENOMEM);
00347 }
00348
00349 offset = avio_rb32(s->pb);
00350 avio_seek(s->pb, offset, SEEK_SET);
00351 avio_skip(s->pb, 4);
00352
00353 s->start_time = 0;
00354
00355 return 0;
00356 }
00357
00358 static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
00359 {
00360 av_free(st->codec->extradata);
00361 st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
00362 if (!st->codec->extradata)
00363 return AVERROR(ENOMEM);
00364 st->codec->extradata_size = size;
00365 avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
00366 return 0;
00367 }
00368
00369 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
00370 {
00371 FLVContext *flv = s->priv_data;
00372 int ret, i, type, size, flags, is_audio;
00373 int64_t next, pos;
00374 int64_t dts, pts = AV_NOPTS_VALUE;
00375 AVStream *st = NULL;
00376
00377 for(;;avio_skip(s->pb, 4)){
00378 pos = avio_tell(s->pb);
00379 type = avio_r8(s->pb);
00380 size = avio_rb24(s->pb);
00381 dts = avio_rb24(s->pb);
00382 dts |= avio_r8(s->pb) << 24;
00383 av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
00384 if (url_feof(s->pb))
00385 return AVERROR_EOF;
00386 avio_skip(s->pb, 3);
00387 flags = 0;
00388
00389 if(size == 0)
00390 continue;
00391
00392 next= size + avio_tell(s->pb);
00393
00394 if (type == FLV_TAG_TYPE_AUDIO) {
00395 is_audio=1;
00396 flags = avio_r8(s->pb);
00397 size--;
00398 } else if (type == FLV_TAG_TYPE_VIDEO) {
00399 is_audio=0;
00400 flags = avio_r8(s->pb);
00401 size--;
00402 if ((flags & 0xf0) == 0x50)
00403 goto skip;
00404 } else {
00405 if (type == FLV_TAG_TYPE_META && size > 13+1+4)
00406 flv_read_metabody(s, next);
00407 else
00408 av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
00409 skip:
00410 avio_seek(s->pb, next, SEEK_SET);
00411 continue;
00412 }
00413
00414
00415 if (!size)
00416 continue;
00417
00418
00419 for(i=0;i<s->nb_streams;i++) {
00420 st = s->streams[i];
00421 if (st->id == is_audio)
00422 break;
00423 }
00424 if(i == s->nb_streams){
00425 av_log(s, AV_LOG_ERROR, "invalid stream\n");
00426 st= create_stream(s, is_audio);
00427 s->ctx_flags &= ~AVFMTCTX_NOHEADER;
00428 }
00429 av_dlog(s, "%d %X %d \n", is_audio, flags, st->discard);
00430 if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio))
00431 ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
00432 || st->discard >= AVDISCARD_ALL
00433 ){
00434 avio_seek(s->pb, next, SEEK_SET);
00435 continue;
00436 }
00437 if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
00438 av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
00439 break;
00440 }
00441
00442
00443 if(s->pb->seekable && (!s->duration || s->duration==AV_NOPTS_VALUE)){
00444 int size;
00445 const int64_t pos= avio_tell(s->pb);
00446 const int64_t fsize= avio_size(s->pb);
00447 avio_seek(s->pb, fsize-4, SEEK_SET);
00448 size= avio_rb32(s->pb);
00449 avio_seek(s->pb, fsize-3-size, SEEK_SET);
00450 if(size == avio_rb24(s->pb) + 11){
00451 uint32_t ts = avio_rb24(s->pb);
00452 ts |= avio_r8(s->pb) << 24;
00453 s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
00454 }
00455 avio_seek(s->pb, pos, SEEK_SET);
00456 }
00457
00458 if(is_audio){
00459 if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
00460 st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
00461 st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
00462 st->codec->bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
00463 }
00464 if(!st->codec->codec_id){
00465 flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);
00466 }
00467 }else{
00468 size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
00469 }
00470
00471 if (st->codec->codec_id == CODEC_ID_AAC ||
00472 st->codec->codec_id == CODEC_ID_H264) {
00473 int type = avio_r8(s->pb);
00474 size--;
00475 if (st->codec->codec_id == CODEC_ID_H264) {
00476 int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000;
00477 pts = dts + cts;
00478 if (cts < 0) {
00479 flv->wrong_dts = 1;
00480 av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
00481 }
00482 if (flv->wrong_dts)
00483 dts = AV_NOPTS_VALUE;
00484 }
00485 if (type == 0) {
00486 if ((ret = flv_get_extradata(s, st, size)) < 0)
00487 return ret;
00488 if (st->codec->codec_id == CODEC_ID_AAC) {
00489 MPEG4AudioConfig cfg;
00490 ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
00491 st->codec->extradata_size);
00492 st->codec->channels = cfg.channels;
00493 if (cfg.ext_sample_rate)
00494 st->codec->sample_rate = cfg.ext_sample_rate;
00495 else
00496 st->codec->sample_rate = cfg.sample_rate;
00497 av_dlog(s, "mp4a config channels %d sample rate %d\n",
00498 st->codec->channels, st->codec->sample_rate);
00499 }
00500
00501 ret = AVERROR(EAGAIN);
00502 goto leave;
00503 }
00504 }
00505
00506
00507 if (!size) {
00508 ret = AVERROR(EAGAIN);
00509 goto leave;
00510 }
00511
00512 ret= av_get_packet(s->pb, pkt, size);
00513 if (ret < 0) {
00514 return AVERROR(EIO);
00515 }
00516
00517
00518 pkt->size = ret;
00519 pkt->dts = dts;
00520 pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
00521 pkt->stream_index = st->index;
00522
00523 if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
00524 pkt->flags |= AV_PKT_FLAG_KEY;
00525
00526 leave:
00527 avio_skip(s->pb, 4);
00528 return ret;
00529 }
00530
00531 static int flv_read_seek(AVFormatContext *s, int stream_index,
00532 int64_t ts, int flags)
00533 {
00534 return avio_seek_time(s->pb, stream_index, ts, flags);
00535 }
00536
00537 #if 0
00538 static int flv_read_seek2(AVFormatContext *s, int stream_index,
00539 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
00540 {
00541 int ret = AVERROR(ENOSYS);
00542
00543 if (ts - min_ts > (uint64_t)(max_ts - ts)) flags |= AVSEEK_FLAG_BACKWARD;
00544
00545 if (!s->pb->seekable) {
00546 if (stream_index < 0) {
00547 stream_index = av_find_default_stream_index(s);
00548 if (stream_index < 0)
00549 return -1;
00550
00551
00552 ts = av_rescale_rnd(ts, 1000, AV_TIME_BASE,
00553 flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
00554 }
00555 ret = avio_seek_time(s->pb, stream_index, ts, flags);
00556 }
00557
00558 if (ret == AVERROR(ENOSYS))
00559 ret = av_seek_frame(s, stream_index, ts, flags);
00560 return ret;
00561 }
00562 #endif
00563
00564 AVInputFormat ff_flv_demuxer = {
00565 "flv",
00566 NULL_IF_CONFIG_SMALL("FLV format"),
00567 sizeof(FLVContext),
00568 flv_probe,
00569 flv_read_header,
00570 flv_read_packet,
00571 .read_seek = flv_read_seek,
00572 #if 0
00573 .read_seek2 = flv_read_seek2,
00574 #endif
00575 .extensions = "flv",
00576 .value = CODEC_ID_FLV1,
00577 };