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 "libavutil/opt.h"
00030 #include "libavutil/intfloat.h"
00031 #include "libavutil/mathematics.h"
00032 #include "libavcodec/bytestream.h"
00033 #include "libavcodec/mpeg4audio.h"
00034 #include "avformat.h"
00035 #include "internal.h"
00036 #include "avio_internal.h"
00037 #include "flv.h"
00038
00039 #define VALIDATE_INDEX_TS_THRESH 2500
00040
00041 typedef struct {
00042 const AVClass *class;
00043 int trust_metadata;
00044 int wrong_dts;
00045 uint8_t *new_extradata[FLV_STREAM_TYPE_NB];
00046 int new_extradata_size[FLV_STREAM_TYPE_NB];
00047 int last_sample_rate;
00048 int last_channels;
00049 struct {
00050 int64_t dts;
00051 int64_t pos;
00052 } validate_index[2];
00053 int validate_next;
00054 int validate_count;
00055 int searched_for_end;
00056 } FLVContext;
00057
00058 static int flv_probe(AVProbeData *p)
00059 {
00060 const uint8_t *d;
00061
00062 d = p->buf;
00063 if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
00064 return AVPROBE_SCORE_MAX;
00065 }
00066 return 0;
00067 }
00068
00069 static AVStream *create_stream(AVFormatContext *s, int codec_type)
00070 {
00071 AVStream *st = avformat_new_stream(s, NULL);
00072 if (!st)
00073 return NULL;
00074 st->codec->codec_type = codec_type;
00075 if(s->nb_streams>=3 ||( s->nb_streams==2
00076 && s->streams[0]->codec->codec_type != AVMEDIA_TYPE_DATA
00077 && s->streams[1]->codec->codec_type != AVMEDIA_TYPE_DATA))
00078 s->ctx_flags &= ~AVFMTCTX_NOHEADER;
00079
00080 avpriv_set_pts_info(st, 32, 1, 1000);
00081 return st;
00082 }
00083 static int flv_same_audio_codec(AVCodecContext *acodec, int flags)
00084 {
00085 int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
00086 int flv_codecid = flags & FLV_AUDIO_CODECID_MASK;
00087 int codec_id;
00088
00089 if (!acodec->codec_id && !acodec->codec_tag)
00090 return 1;
00091
00092 if (acodec->bits_per_coded_sample != bits_per_coded_sample)
00093 return 0;
00094
00095 switch(flv_codecid) {
00096
00097 case FLV_CODECID_PCM:
00098 codec_id = bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 :
00099 #if HAVE_BIGENDIAN
00100 AV_CODEC_ID_PCM_S16BE;
00101 #else
00102 AV_CODEC_ID_PCM_S16LE;
00103 #endif
00104 return codec_id == acodec->codec_id;
00105 case FLV_CODECID_PCM_LE:
00106 codec_id = bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 : AV_CODEC_ID_PCM_S16LE;
00107 return codec_id == acodec->codec_id;
00108 case FLV_CODECID_AAC:
00109 return acodec->codec_id == AV_CODEC_ID_AAC;
00110 case FLV_CODECID_ADPCM:
00111 return acodec->codec_id == AV_CODEC_ID_ADPCM_SWF;
00112 case FLV_CODECID_SPEEX:
00113 return acodec->codec_id == AV_CODEC_ID_SPEEX;
00114 case FLV_CODECID_MP3:
00115 return acodec->codec_id == AV_CODEC_ID_MP3;
00116 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
00117 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
00118 case FLV_CODECID_NELLYMOSER:
00119 return acodec->codec_id == AV_CODEC_ID_NELLYMOSER;
00120 case FLV_CODECID_PCM_MULAW:
00121 return acodec->sample_rate == 8000 &&
00122 acodec->codec_id == AV_CODEC_ID_PCM_MULAW;
00123 case FLV_CODECID_PCM_ALAW:
00124 return acodec->sample_rate = 8000 &&
00125 acodec->codec_id == AV_CODEC_ID_PCM_ALAW;
00126 default:
00127 return acodec->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
00128 }
00129 }
00130
00131 static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, AVCodecContext *acodec, int flv_codecid) {
00132 switch(flv_codecid) {
00133
00134 case FLV_CODECID_PCM:
00135 acodec->codec_id = acodec->bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 :
00136 #if HAVE_BIGENDIAN
00137 AV_CODEC_ID_PCM_S16BE;
00138 #else
00139 AV_CODEC_ID_PCM_S16LE;
00140 #endif
00141 break;
00142 case FLV_CODECID_PCM_LE:
00143 acodec->codec_id = acodec->bits_per_coded_sample == 8 ? AV_CODEC_ID_PCM_U8 : AV_CODEC_ID_PCM_S16LE; break;
00144 case FLV_CODECID_AAC : acodec->codec_id = AV_CODEC_ID_AAC; break;
00145 case FLV_CODECID_ADPCM: acodec->codec_id = AV_CODEC_ID_ADPCM_SWF; break;
00146 case FLV_CODECID_SPEEX:
00147 acodec->codec_id = AV_CODEC_ID_SPEEX;
00148 acodec->sample_rate = 16000;
00149 break;
00150 case FLV_CODECID_MP3 : acodec->codec_id = AV_CODEC_ID_MP3 ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
00151 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
00152 acodec->sample_rate = 8000;
00153 acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
00154 break;
00155 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
00156 acodec->sample_rate = 16000;
00157 acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
00158 break;
00159 case FLV_CODECID_NELLYMOSER:
00160 acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
00161 break;
00162 case FLV_CODECID_PCM_MULAW:
00163 acodec->sample_rate = 8000;
00164 acodec->codec_id = AV_CODEC_ID_PCM_MULAW;
00165 break;
00166 case FLV_CODECID_PCM_ALAW:
00167 acodec->sample_rate = 8000;
00168 acodec->codec_id = AV_CODEC_ID_PCM_ALAW;
00169 break;
00170 default:
00171 av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
00172 acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
00173 }
00174 }
00175
00176 static int flv_same_video_codec(AVCodecContext *vcodec, int flags)
00177 {
00178 int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;
00179
00180 if (!vcodec->codec_id && !vcodec->codec_tag)
00181 return 1;
00182
00183 switch (flv_codecid) {
00184 case FLV_CODECID_H263:
00185 return vcodec->codec_id == AV_CODEC_ID_FLV1;
00186 case FLV_CODECID_SCREEN:
00187 return vcodec->codec_id == AV_CODEC_ID_FLASHSV;
00188 case FLV_CODECID_SCREEN2:
00189 return vcodec->codec_id == AV_CODEC_ID_FLASHSV2;
00190 case FLV_CODECID_VP6:
00191 return vcodec->codec_id == AV_CODEC_ID_VP6F;
00192 case FLV_CODECID_VP6A:
00193 return vcodec->codec_id == AV_CODEC_ID_VP6A;
00194 case FLV_CODECID_H264:
00195 return vcodec->codec_id == AV_CODEC_ID_H264;
00196 default:
00197 return vcodec->codec_tag == flv_codecid;
00198 }
00199 }
00200
00201 static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
00202 AVCodecContext *vcodec = vstream->codec;
00203 switch(flv_codecid) {
00204 case FLV_CODECID_H263 : vcodec->codec_id = AV_CODEC_ID_FLV1 ; break;
00205 case FLV_CODECID_REALH263: vcodec->codec_id = AV_CODEC_ID_H263 ; break;
00206 case FLV_CODECID_SCREEN: vcodec->codec_id = AV_CODEC_ID_FLASHSV; break;
00207 case FLV_CODECID_SCREEN2: vcodec->codec_id = AV_CODEC_ID_FLASHSV2; break;
00208 case FLV_CODECID_VP6 : vcodec->codec_id = AV_CODEC_ID_VP6F ;
00209 case FLV_CODECID_VP6A :
00210 if(flv_codecid == FLV_CODECID_VP6A)
00211 vcodec->codec_id = AV_CODEC_ID_VP6A;
00212 if(vcodec->extradata_size != 1) {
00213 vcodec->extradata_size = 1;
00214 vcodec->extradata = av_malloc(1 + FF_INPUT_BUFFER_PADDING_SIZE);
00215 }
00216 vcodec->extradata[0] = avio_r8(s->pb);
00217 return 1;
00218 case FLV_CODECID_H264:
00219 vcodec->codec_id = AV_CODEC_ID_H264;
00220 return 3;
00221 case FLV_CODECID_MPEG4:
00222 vcodec->codec_id = AV_CODEC_ID_MPEG4;
00223 return 3;
00224 default:
00225 av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
00226 vcodec->codec_tag = flv_codecid;
00227 }
00228
00229 return 0;
00230 }
00231
00232 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize) {
00233 int length = avio_rb16(ioc);
00234 if(length >= buffsize) {
00235 avio_skip(ioc, length);
00236 return -1;
00237 }
00238
00239 avio_read(ioc, buffer, length);
00240
00241 buffer[length] = '\0';
00242
00243 return length;
00244 }
00245
00246 static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, AVStream *vstream, int64_t max_pos) {
00247 FLVContext *flv = s->priv_data;
00248 unsigned int timeslen = 0, fileposlen = 0, i;
00249 char str_val[256];
00250 int64_t *times = NULL;
00251 int64_t *filepositions = NULL;
00252 int ret = AVERROR(ENOSYS);
00253 int64_t initial_pos = avio_tell(ioc);
00254
00255 if(vstream->nb_index_entries>0){
00256 av_log(s, AV_LOG_WARNING, "Skiping duplicate index\n");
00257 return 0;
00258 }
00259
00260 if (s->flags & AVFMT_FLAG_IGNIDX)
00261 return 0;
00262
00263 while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
00264 int64_t** current_array;
00265 unsigned int arraylen;
00266
00267
00268 if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
00269 break;
00270
00271 arraylen = avio_rb32(ioc);
00272 if(arraylen>>28)
00273 break;
00274
00275 if (!strcmp(KEYFRAMES_TIMESTAMP_TAG , str_val) && !times){
00276 current_array= ×
00277 timeslen= arraylen;
00278 }else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) && !filepositions){
00279 current_array= &filepositions;
00280 fileposlen= arraylen;
00281 }else
00282 break;
00283
00284 if (!(*current_array = av_mallocz(sizeof(**current_array) * arraylen))) {
00285 ret = AVERROR(ENOMEM);
00286 goto finish;
00287 }
00288
00289 for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
00290 if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
00291 goto invalid;
00292 current_array[0][i] = av_int2double(avio_rb64(ioc));
00293 }
00294 if (times && filepositions) {
00295
00296
00297 ret = 0;
00298 break;
00299 }
00300 }
00301
00302 if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
00303 for (i = 0; i < fileposlen; i++) {
00304 av_add_index_entry(vstream, filepositions[i], times[i]*1000,
00305 0, 0, AVINDEX_KEYFRAME);
00306 if (i < 2) {
00307 flv->validate_index[i].pos = filepositions[i];
00308 flv->validate_index[i].dts = times[i] * 1000;
00309 flv->validate_count = i + 1;
00310 }
00311 }
00312 } else {
00313 invalid:
00314 av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
00315 }
00316
00317 finish:
00318 av_freep(×);
00319 av_freep(&filepositions);
00320 avio_seek(ioc, initial_pos, SEEK_SET);
00321 return ret;
00322 }
00323
00324 static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
00325 AVCodecContext *acodec, *vcodec;
00326 FLVContext *flv = s->priv_data;
00327 AVIOContext *ioc;
00328 AMFDataType amf_type;
00329 char str_val[256];
00330 double num_val;
00331
00332 num_val = 0;
00333 ioc = s->pb;
00334
00335 amf_type = avio_r8(ioc);
00336
00337 switch(amf_type) {
00338 case AMF_DATA_TYPE_NUMBER:
00339 num_val = av_int2double(avio_rb64(ioc)); break;
00340 case AMF_DATA_TYPE_BOOL:
00341 num_val = avio_r8(ioc); break;
00342 case AMF_DATA_TYPE_STRING:
00343 if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
00344 return -1;
00345 break;
00346 case AMF_DATA_TYPE_OBJECT:
00347 if ((vstream || astream) && ioc->seekable && key && !strcmp(KEYFRAMES_TAG, key) && depth == 1)
00348 if (parse_keyframes_index(s, ioc, vstream ? vstream : astream,
00349 max_pos) < 0)
00350 av_log(s, AV_LOG_ERROR, "Keyframe index parsing failed\n");
00351
00352 while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
00353 if (amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
00354 return -1;
00355 }
00356 if(avio_r8(ioc) != AMF_END_OF_OBJECT)
00357 return -1;
00358 break;
00359 case AMF_DATA_TYPE_NULL:
00360 case AMF_DATA_TYPE_UNDEFINED:
00361 case AMF_DATA_TYPE_UNSUPPORTED:
00362 break;
00363 case AMF_DATA_TYPE_MIXEDARRAY:
00364 avio_skip(ioc, 4);
00365 while(avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
00366
00367 if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
00368 return -1;
00369 }
00370 if(avio_r8(ioc) != AMF_END_OF_OBJECT)
00371 return -1;
00372 break;
00373 case AMF_DATA_TYPE_ARRAY: {
00374 unsigned int arraylen, i;
00375
00376 arraylen = avio_rb32(ioc);
00377 for(i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
00378 if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
00379 return -1;
00380 }
00381 }
00382 break;
00383 case AMF_DATA_TYPE_DATE:
00384 avio_skip(ioc, 8 + 2);
00385 break;
00386 default:
00387 return -1;
00388 }
00389
00390 if(depth == 1 && key) {
00391 acodec = astream ? astream->codec : NULL;
00392 vcodec = vstream ? vstream->codec : NULL;
00393
00394 if (amf_type == AMF_DATA_TYPE_NUMBER) {
00395 if (!strcmp(key, "duration"))
00396 s->duration = num_val * AV_TIME_BASE;
00397 else if (!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
00398 vcodec->bit_rate = num_val * 1024.0;
00399 else if (!strcmp(key, "audiodatarate") && acodec && 0 <= (int)(num_val * 1024.0))
00400 acodec->bit_rate = num_val * 1024.0;
00401 else if (!strcmp(key, "datastream")) {
00402 AVStream *st = create_stream(s, AVMEDIA_TYPE_DATA);
00403 if (!st)
00404 return AVERROR(ENOMEM);
00405 st->codec->codec_id = AV_CODEC_ID_TEXT;
00406 } else if (flv->trust_metadata) {
00407 if (!strcmp(key, "videocodecid") && vcodec) {
00408 flv_set_video_codec(s, vstream, num_val);
00409 } else
00410 if (!strcmp(key, "audiocodecid") && acodec) {
00411 flv_set_audio_codec(s, astream, acodec, num_val);
00412 } else
00413 if (!strcmp(key, "audiosamplerate") && acodec) {
00414 acodec->sample_rate = num_val;
00415 } else
00416 if (!strcmp(key, "width") && vcodec) {
00417 vcodec->width = num_val;
00418 } else
00419 if (!strcmp(key, "height") && vcodec) {
00420 vcodec->height = num_val;
00421 }
00422 }
00423 }
00424
00425 if (amf_type == AMF_DATA_TYPE_OBJECT && s->nb_streams == 1 &&
00426 ((!acodec && !strcmp(key, "audiocodecid")) ||
00427 (!vcodec && !strcmp(key, "videocodecid"))))
00428 s->ctx_flags &= ~AVFMTCTX_NOHEADER;
00429
00430 if (!strcmp(key, "duration") ||
00431 !strcmp(key, "filesize") ||
00432 !strcmp(key, "width") ||
00433 !strcmp(key, "height") ||
00434 !strcmp(key, "videodatarate") ||
00435 !strcmp(key, "framerate") ||
00436 !strcmp(key, "videocodecid") ||
00437 !strcmp(key, "audiodatarate") ||
00438 !strcmp(key, "audiosamplerate") ||
00439 !strcmp(key, "audiosamplesize") ||
00440 !strcmp(key, "stereo") ||
00441 !strcmp(key, "audiocodecid"))
00442 return 0;
00443
00444 if(amf_type == AMF_DATA_TYPE_BOOL) {
00445 av_strlcpy(str_val, num_val > 0 ? "true" : "false", sizeof(str_val));
00446 av_dict_set(&s->metadata, key, str_val, 0);
00447 } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
00448 snprintf(str_val, sizeof(str_val), "%.f", num_val);
00449 av_dict_set(&s->metadata, key, str_val, 0);
00450 } else if (amf_type == AMF_DATA_TYPE_STRING)
00451 av_dict_set(&s->metadata, key, str_val, 0);
00452 }
00453
00454 return 0;
00455 }
00456
00457 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
00458 AMFDataType type;
00459 AVStream *stream, *astream, *vstream, *dstream;
00460 AVIOContext *ioc;
00461 int i;
00462 char buffer[11];
00463
00464 vstream = astream = dstream = NULL;
00465 ioc = s->pb;
00466
00467
00468 type = avio_r8(ioc);
00469 if (type != AMF_DATA_TYPE_STRING ||
00470 amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
00471 return -1;
00472
00473 if (!strcmp(buffer, "onTextData"))
00474 return 1;
00475
00476 if (strcmp(buffer, "onMetaData"))
00477 return -1;
00478
00479
00480 for(i = 0; i < s->nb_streams; i++) {
00481 stream = s->streams[i];
00482 if(stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) vstream = stream;
00483 else if(stream->codec->codec_type == AVMEDIA_TYPE_AUDIO) astream = stream;
00484 else if(stream->codec->codec_type == AVMEDIA_TYPE_DATA) dstream = stream;
00485 }
00486
00487
00488 if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
00489 return -1;
00490
00491 return 0;
00492 }
00493
00494 static int flv_read_header(AVFormatContext *s)
00495 {
00496 int offset, flags;
00497
00498 avio_skip(s->pb, 4);
00499 flags = avio_r8(s->pb);
00500
00501
00502 if (!flags) {
00503 flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
00504 av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
00505 }
00506
00507 s->ctx_flags |= AVFMTCTX_NOHEADER;
00508
00509 if(flags & FLV_HEADER_FLAG_HASVIDEO){
00510 if(!create_stream(s, AVMEDIA_TYPE_VIDEO))
00511 return AVERROR(ENOMEM);
00512 }
00513 if(flags & FLV_HEADER_FLAG_HASAUDIO){
00514 if(!create_stream(s, AVMEDIA_TYPE_AUDIO))
00515 return AVERROR(ENOMEM);
00516 }
00517
00518
00519
00520 offset = avio_rb32(s->pb);
00521 avio_seek(s->pb, offset, SEEK_SET);
00522 avio_skip(s->pb, 4);
00523
00524 s->start_time = 0;
00525
00526 return 0;
00527 }
00528
00529 static int flv_read_close(AVFormatContext *s)
00530 {
00531 int i;
00532 FLVContext *flv = s->priv_data;
00533 for(i=0; i<FLV_STREAM_TYPE_NB; i++)
00534 av_freep(&flv->new_extradata[i]);
00535 return 0;
00536 }
00537
00538 static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
00539 {
00540 av_free(st->codec->extradata);
00541 st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
00542 if (!st->codec->extradata)
00543 return AVERROR(ENOMEM);
00544 st->codec->extradata_size = size;
00545 avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
00546 return 0;
00547 }
00548
00549 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
00550 int size)
00551 {
00552 av_free(flv->new_extradata[stream]);
00553 flv->new_extradata[stream] = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
00554 if (!flv->new_extradata[stream])
00555 return AVERROR(ENOMEM);
00556 flv->new_extradata_size[stream] = size;
00557 avio_read(pb, flv->new_extradata[stream], size);
00558 return 0;
00559 }
00560
00561 static void clear_index_entries(AVFormatContext *s, int64_t pos)
00562 {
00563 int i, j, out;
00564 av_log(s, AV_LOG_WARNING, "Found invalid index entries, clearing the index.\n");
00565 for (i = 0; i < s->nb_streams; i++) {
00566 AVStream *st = s->streams[i];
00567
00568 out = 0;
00569 for (j = 0; j < st->nb_index_entries; j++) {
00570 if (st->index_entries[j].pos < pos)
00571 st->index_entries[out++] = st->index_entries[j];
00572 }
00573 st->nb_index_entries = out;
00574 }
00575 }
00576
00577
00578 static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
00579 int64_t dts, int64_t next)
00580 {
00581 int ret = AVERROR_INVALIDDATA, i;
00582 AVIOContext *pb = s->pb;
00583 AVStream *st = NULL;
00584 AMFDataType type;
00585 char buf[20];
00586 int length;
00587
00588 type = avio_r8(pb);
00589 if (type == AMF_DATA_TYPE_MIXEDARRAY)
00590 avio_seek(pb, 4, SEEK_CUR);
00591 else if (type != AMF_DATA_TYPE_OBJECT)
00592 goto out;
00593
00594 amf_get_string(pb, buf, sizeof(buf));
00595 if (strcmp(buf, "type") || avio_r8(pb) != AMF_DATA_TYPE_STRING)
00596 goto out;
00597
00598 amf_get_string(pb, buf, sizeof(buf));
00599
00600 amf_get_string(pb, buf, sizeof(buf));
00601 if (strcmp(buf, "text") || avio_r8(pb) != AMF_DATA_TYPE_STRING)
00602 goto out;
00603
00604 length = avio_rb16(pb);
00605 ret = av_get_packet(s->pb, pkt, length);
00606 if (ret < 0) {
00607 ret = AVERROR(EIO);
00608 goto out;
00609 }
00610
00611 for (i = 0; i < s->nb_streams; i++) {
00612 st = s->streams[i];
00613 if (st->codec->codec_type == AVMEDIA_TYPE_DATA)
00614 break;
00615 }
00616
00617 if (i == s->nb_streams) {
00618 st = create_stream(s, AVMEDIA_TYPE_DATA);
00619 if (!st)
00620 goto out;
00621 st->codec->codec_id = AV_CODEC_ID_TEXT;
00622 }
00623
00624 pkt->dts = dts;
00625 pkt->pts = dts;
00626 pkt->size = ret;
00627
00628 pkt->stream_index = st->index;
00629 pkt->flags |= AV_PKT_FLAG_KEY;
00630
00631 avio_seek(s->pb, next + 4, SEEK_SET);
00632 out:
00633 return ret;
00634 }
00635
00636 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
00637 {
00638 FLVContext *flv = s->priv_data;
00639 int ret, i, type, size, flags;
00640 int stream_type=-1;
00641 int64_t next, pos;
00642 int64_t dts, pts = AV_NOPTS_VALUE;
00643 int av_uninit(channels);
00644 int av_uninit(sample_rate);
00645 AVStream *st = NULL;
00646
00647 for(;;avio_skip(s->pb, 4)){
00648 pos = avio_tell(s->pb);
00649 type = avio_r8(s->pb);
00650 size = avio_rb24(s->pb);
00651 dts = avio_rb24(s->pb);
00652 dts |= avio_r8(s->pb) << 24;
00653 av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
00654 if (url_feof(s->pb))
00655 return AVERROR_EOF;
00656 avio_skip(s->pb, 3);
00657 flags = 0;
00658
00659 if (flv->validate_next < flv->validate_count) {
00660 int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
00661 if (pos == validate_pos) {
00662 if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
00663 VALIDATE_INDEX_TS_THRESH) {
00664 flv->validate_next++;
00665 } else {
00666 clear_index_entries(s, validate_pos);
00667 flv->validate_count = 0;
00668 }
00669 } else if (pos > validate_pos) {
00670 clear_index_entries(s, validate_pos);
00671 flv->validate_count = 0;
00672 }
00673 }
00674
00675 if(size == 0)
00676 continue;
00677
00678 next= size + avio_tell(s->pb);
00679
00680 if (type == FLV_TAG_TYPE_AUDIO) {
00681 stream_type=FLV_STREAM_TYPE_AUDIO;
00682 flags = avio_r8(s->pb);
00683 size--;
00684 } else if (type == FLV_TAG_TYPE_VIDEO) {
00685 stream_type=FLV_STREAM_TYPE_VIDEO;
00686 flags = avio_r8(s->pb);
00687 size--;
00688 if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_VIDEO_INFO_CMD)
00689 goto skip;
00690 } else if (type == FLV_TAG_TYPE_META) {
00691 if (size > 13+1+4 && dts == 0) {
00692 flv_read_metabody(s, next);
00693 goto skip;
00694 } else if (dts != 0) {
00695 stream_type=FLV_STREAM_TYPE_DATA;
00696 } else {
00697 goto skip;
00698 }
00699 } else {
00700 av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
00701 skip:
00702 avio_seek(s->pb, next, SEEK_SET);
00703 continue;
00704 }
00705
00706
00707 if (!size)
00708 continue;
00709
00710
00711 for(i=0;i<s->nb_streams;i++) {
00712 st = s->streams[i];
00713 if (stream_type == FLV_STREAM_TYPE_AUDIO) {
00714 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
00715 flv_same_audio_codec(st->codec, flags)) {
00716 break;
00717 }
00718 } else
00719 if (stream_type == FLV_STREAM_TYPE_VIDEO) {
00720 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
00721 flv_same_video_codec(st->codec, flags)) {
00722 break;
00723 }
00724 } else if (stream_type == FLV_STREAM_TYPE_DATA) {
00725 if (st->codec->codec_type == AVMEDIA_TYPE_DATA)
00726 break;
00727 }
00728 }
00729 if(i == s->nb_streams){
00730 av_log(s, AV_LOG_WARNING, "Stream discovered after head already parsed\n");
00731 st = create_stream(s,
00732 (int[]){AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_DATA}[stream_type]);
00733
00734 }
00735 av_dlog(s, "%d %X %d \n", stream_type, flags, st->discard);
00736 if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || (stream_type == FLV_STREAM_TYPE_AUDIO)))
00737 ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && (stream_type == FLV_STREAM_TYPE_VIDEO)))
00738 || st->discard >= AVDISCARD_ALL
00739 ){
00740 avio_seek(s->pb, next, SEEK_SET);
00741 continue;
00742 }
00743 if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
00744 av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
00745 break;
00746 }
00747
00748
00749 if(s->pb->seekable && (!s->duration || s->duration==AV_NOPTS_VALUE) && !flv->searched_for_end){
00750 int size;
00751 const int64_t pos= avio_tell(s->pb);
00752 int64_t fsize= avio_size(s->pb);
00753 retry_duration:
00754 avio_seek(s->pb, fsize-4, SEEK_SET);
00755 size= avio_rb32(s->pb);
00756 avio_seek(s->pb, fsize-3-size, SEEK_SET);
00757 if(size == avio_rb24(s->pb) + 11){
00758 uint32_t ts = avio_rb24(s->pb);
00759 ts |= avio_r8(s->pb) << 24;
00760 if(ts)
00761 s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
00762 else if (fsize >= 8 && fsize - 8 >= size){
00763 fsize -= size+4;
00764 goto retry_duration;
00765 }
00766 }
00767
00768 avio_seek(s->pb, pos, SEEK_SET);
00769 flv->searched_for_end = 1;
00770 }
00771
00772 if(stream_type == FLV_STREAM_TYPE_AUDIO){
00773 int bits_per_coded_sample;
00774 channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
00775 sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
00776 bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
00777 if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
00778 st->codec->channels = channels;
00779 st->codec->sample_rate = sample_rate;
00780 st->codec->bits_per_coded_sample = bits_per_coded_sample;
00781 }
00782 if(!st->codec->codec_id){
00783 flv_set_audio_codec(s, st, st->codec, flags & FLV_AUDIO_CODECID_MASK);
00784 flv->last_sample_rate = sample_rate = st->codec->sample_rate;
00785 flv->last_channels = channels = st->codec->channels;
00786 } else {
00787 AVCodecContext ctx;
00788 ctx.sample_rate = sample_rate;
00789 flv_set_audio_codec(s, st, &ctx, flags & FLV_AUDIO_CODECID_MASK);
00790 sample_rate = ctx.sample_rate;
00791 }
00792 } else if(stream_type == FLV_STREAM_TYPE_VIDEO) {
00793 size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
00794 }
00795
00796 if (st->codec->codec_id == AV_CODEC_ID_AAC ||
00797 st->codec->codec_id == AV_CODEC_ID_H264 ||
00798 st->codec->codec_id == AV_CODEC_ID_MPEG4) {
00799 int type = avio_r8(s->pb);
00800 size--;
00801 if (st->codec->codec_id == AV_CODEC_ID_H264 || st->codec->codec_id == AV_CODEC_ID_MPEG4) {
00802 int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000;
00803 pts = dts + cts;
00804 if (cts < 0) {
00805 flv->wrong_dts = 1;
00806 av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
00807 }
00808 if (flv->wrong_dts)
00809 dts = AV_NOPTS_VALUE;
00810 }
00811 if (type == 0 && (!st->codec->extradata || st->codec->codec_id == AV_CODEC_ID_AAC)) {
00812 if (st->codec->extradata) {
00813 if ((ret = flv_queue_extradata(flv, s->pb, stream_type, size)) < 0)
00814 return ret;
00815 ret = AVERROR(EAGAIN);
00816 goto leave;
00817 }
00818 if ((ret = flv_get_extradata(s, st, size)) < 0)
00819 return ret;
00820 if (st->codec->codec_id == AV_CODEC_ID_AAC) {
00821 MPEG4AudioConfig cfg;
00822 if (avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
00823 st->codec->extradata_size * 8, 1) >= 0) {
00824 st->codec->channels = cfg.channels;
00825 if (cfg.ext_sample_rate)
00826 st->codec->sample_rate = cfg.ext_sample_rate;
00827 else
00828 st->codec->sample_rate = cfg.sample_rate;
00829 av_dlog(s, "mp4a config channels %d sample rate %d\n",
00830 st->codec->channels, st->codec->sample_rate);
00831 }
00832 }
00833
00834 ret = AVERROR(EAGAIN);
00835 goto leave;
00836 }
00837 }
00838
00839
00840 if (!size) {
00841 ret = AVERROR(EAGAIN);
00842 goto leave;
00843 }
00844
00845 ret= av_get_packet(s->pb, pkt, size);
00846 if (ret < 0)
00847 return ret;
00848 pkt->dts = dts;
00849 pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
00850 pkt->stream_index = st->index;
00851 if (flv->new_extradata[stream_type]) {
00852 uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
00853 flv->new_extradata_size[stream_type]);
00854 if (side) {
00855 memcpy(side, flv->new_extradata[stream_type],
00856 flv->new_extradata_size[stream_type]);
00857 av_freep(&flv->new_extradata[stream_type]);
00858 flv->new_extradata_size[stream_type] = 0;
00859 }
00860 }
00861 if (stream_type == FLV_STREAM_TYPE_AUDIO && (sample_rate != flv->last_sample_rate ||
00862 channels != flv->last_channels)) {
00863 flv->last_sample_rate = sample_rate;
00864 flv->last_channels = channels;
00865 ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
00866 }
00867
00868 if ( stream_type == FLV_STREAM_TYPE_AUDIO ||
00869 ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY) ||
00870 stream_type == FLV_STREAM_TYPE_DATA)
00871 pkt->flags |= AV_PKT_FLAG_KEY;
00872
00873 leave:
00874 avio_skip(s->pb, 4);
00875 return ret;
00876 }
00877
00878 static int flv_read_seek(AVFormatContext *s, int stream_index,
00879 int64_t ts, int flags)
00880 {
00881 FLVContext *flv = s->priv_data;
00882 flv->validate_count = 0;
00883 return avio_seek_time(s->pb, stream_index, ts, flags);
00884 }
00885
00886 #define OFFSET(x) offsetof(FLVContext, x)
00887 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
00888 static const AVOption options[] = {
00889 { "flv_metadata", "Allocate streams according the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD},
00890 { NULL }
00891 };
00892
00893 static const AVClass class = {
00894 .class_name = "flvdec",
00895 .item_name = av_default_item_name,
00896 .option = options,
00897 .version = LIBAVUTIL_VERSION_INT,
00898 };
00899
00900 AVInputFormat ff_flv_demuxer = {
00901 .name = "flv",
00902 .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
00903 .priv_data_size = sizeof(FLVContext),
00904 .read_probe = flv_probe,
00905 .read_header = flv_read_header,
00906 .read_packet = flv_read_packet,
00907 .read_seek = flv_read_seek,
00908 .read_close = flv_read_close,
00909 .extensions = "flv",
00910 .priv_class = &class,
00911 };