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