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