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