• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/flvdec.c

Go to the documentation of this file.
00001 /*
00002  * FLV demuxer
00003  * Copyright (c) 2003 The Libav Project
00004  *
00005  * This demuxer will generate a 1 byte extradata for VP6F content.
00006  * It is composed of:
00007  *  - upper 4bits: difference between encoded width and visible width
00008  *  - lower 4bits: difference between encoded height and visible height
00009  *
00010  * This file is part of Libav.
00011  *
00012  * Libav is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU Lesser General Public
00014  * License as published by the Free Software Foundation; either
00015  * version 2.1 of the License, or (at your option) any later version.
00016  *
00017  * Libav is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * Lesser General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU Lesser General Public
00023  * License along with Libav; if not, write to the Free Software
00024  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 KEYFRAMES_TAG            "keyframes"
00039 #define KEYFRAMES_TIMESTAMP_TAG  "times"
00040 #define KEYFRAMES_BYTEOFFSET_TAG "filepositions"
00041 
00042 typedef struct {
00043     int wrong_dts; 
00044     uint8_t *new_extradata[2];
00045     int      new_extradata_size[2];
00046     int      last_sample_rate;
00047     int      last_channels;
00048 } FLVContext;
00049 
00050 static int flv_probe(AVProbeData *p)
00051 {
00052     const uint8_t *d;
00053 
00054     d = p->buf;
00055     if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
00056         return AVPROBE_SCORE_MAX;
00057     }
00058     return 0;
00059 }
00060 
00061 static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, AVCodecContext *acodec, int flv_codecid) {
00062     switch(flv_codecid) {
00063         //no distinction between S16 and S8 PCM codec flags
00064         case FLV_CODECID_PCM:
00065             acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 :
00066 #if HAVE_BIGENDIAN
00067                                 CODEC_ID_PCM_S16BE;
00068 #else
00069                                 CODEC_ID_PCM_S16LE;
00070 #endif
00071             break;
00072         case FLV_CODECID_PCM_LE:
00073             acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 : CODEC_ID_PCM_S16LE; break;
00074         case FLV_CODECID_AAC  : acodec->codec_id = CODEC_ID_AAC;                                    break;
00075         case FLV_CODECID_ADPCM: acodec->codec_id = CODEC_ID_ADPCM_SWF;                              break;
00076         case FLV_CODECID_SPEEX:
00077             acodec->codec_id = CODEC_ID_SPEEX;
00078             acodec->sample_rate = 16000;
00079             break;
00080         case FLV_CODECID_MP3  : acodec->codec_id = CODEC_ID_MP3      ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
00081         case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
00082             acodec->sample_rate = 8000; //in case metadata does not otherwise declare samplerate
00083             acodec->codec_id = CODEC_ID_NELLYMOSER;
00084             break;
00085         case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
00086             acodec->sample_rate = 16000;
00087             acodec->codec_id = CODEC_ID_NELLYMOSER;
00088             break;
00089         case FLV_CODECID_NELLYMOSER:
00090             acodec->codec_id = CODEC_ID_NELLYMOSER;
00091             break;
00092         default:
00093             av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
00094             acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
00095     }
00096 }
00097 
00098 static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
00099     AVCodecContext *vcodec = vstream->codec;
00100     switch(flv_codecid) {
00101         case FLV_CODECID_H263  : vcodec->codec_id = CODEC_ID_FLV1   ; break;
00102         case FLV_CODECID_SCREEN: vcodec->codec_id = CODEC_ID_FLASHSV; break;
00103         case FLV_CODECID_SCREEN2: vcodec->codec_id = CODEC_ID_FLASHSV2; break;
00104         case FLV_CODECID_VP6   : vcodec->codec_id = CODEC_ID_VP6F   ;
00105         case FLV_CODECID_VP6A  :
00106             if(flv_codecid == FLV_CODECID_VP6A)
00107                 vcodec->codec_id = CODEC_ID_VP6A;
00108             if(vcodec->extradata_size != 1) {
00109                 vcodec->extradata_size = 1;
00110                 vcodec->extradata = av_malloc(1);
00111             }
00112             vcodec->extradata[0] = avio_r8(s->pb);
00113             return 1; // 1 byte body size adjustment for flv_read_packet()
00114         case FLV_CODECID_H264:
00115             vcodec->codec_id = CODEC_ID_H264;
00116             return 3; // not 4, reading packet type will consume one byte
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 arraylen = 0, timeslen = 0, fileposlen = 0, i;
00141     double num_val;
00142     char str_val[256];
00143     int64_t *times = NULL;
00144     int64_t *filepositions = NULL;
00145     int ret = AVERROR(ENOSYS);
00146     int64_t initial_pos = avio_tell(ioc);
00147     AVDictionaryEntry *creator = av_dict_get(s->metadata, "metadatacreator",
00148                                              NULL, 0);
00149 
00150     if (creator && !strcmp(creator->value, "MEGA")) {
00151         /* Files with this metadatacreator tag seem to have filepositions
00152          * pointing at the 4 trailer bytes of the previous packet,
00153          * which isn't the norm (nor what we expect here, nor what
00154          * jwplayer + lighttpd expect, nor what flvtool2 produces).
00155          * Just ignore the index in this case, instead of risking trying
00156          * to adjust it to something that might or might not work. */
00157         return 0;
00158     }
00159 
00160     while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
00161         int64_t* current_array;
00162 
00163         // Expect array object in context
00164         if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
00165             break;
00166 
00167         arraylen = avio_rb32(ioc);
00168         if (arraylen >> 28)
00169             break;
00170 
00171         /*
00172          * Expect only 'times' or 'filepositions' sub-arrays in other case refuse to use such metadata
00173          * for indexing
00174          */
00175         if (!strcmp(KEYFRAMES_TIMESTAMP_TAG, str_val) && !times) {
00176             if (!(times = av_mallocz(sizeof(*times) * arraylen))) {
00177                 ret = AVERROR(ENOMEM);
00178                 goto finish;
00179             }
00180             timeslen = arraylen;
00181             current_array = times;
00182         } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) && !filepositions) {
00183             if (!(filepositions = av_mallocz(sizeof(*filepositions) * arraylen))) {
00184                 ret = AVERROR(ENOMEM);
00185                 goto finish;
00186             }
00187             fileposlen = arraylen;
00188             current_array = filepositions;
00189         } else // unexpected metatag inside keyframes, will not use such metadata for indexing
00190             break;
00191 
00192         for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
00193             if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
00194                 goto finish;
00195             num_val = av_int2double(avio_rb64(ioc));
00196             current_array[i] = num_val;
00197         }
00198         if (times && filepositions) {
00199             // All done, exiting at a position allowing amf_parse_object
00200             // to finish parsing the object
00201             ret = 0;
00202             break;
00203         }
00204     }
00205 
00206     if (!ret && timeslen == fileposlen)
00207          for (i = 0; i < fileposlen; i++)
00208              av_add_index_entry(vstream, filepositions[i], times[i]*1000, 0, 0, AVINDEX_KEYFRAME);
00209     else
00210         av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
00211 
00212 finish:
00213     av_freep(&times);
00214     av_freep(&filepositions);
00215     // If we got unexpected data, but successfully reset back to
00216     // the start pos, the caller can continue parsing
00217     if (ret < 0 && avio_seek(ioc, initial_pos, SEEK_SET) > 0)
00218         return 0;
00219     return ret;
00220 }
00221 
00222 static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
00223     AVCodecContext *acodec, *vcodec;
00224     AVIOContext *ioc;
00225     AMFDataType amf_type;
00226     char str_val[256];
00227     double num_val;
00228 
00229     num_val = 0;
00230     ioc = s->pb;
00231 
00232     amf_type = avio_r8(ioc);
00233 
00234     switch(amf_type) {
00235         case AMF_DATA_TYPE_NUMBER:
00236             num_val = av_int2double(avio_rb64(ioc)); break;
00237         case AMF_DATA_TYPE_BOOL:
00238             num_val = avio_r8(ioc); break;
00239         case AMF_DATA_TYPE_STRING:
00240             if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
00241                 return -1;
00242             break;
00243         case AMF_DATA_TYPE_OBJECT: {
00244             unsigned int keylen;
00245 
00246             if ((vstream || astream) && key && !strcmp(KEYFRAMES_TAG, key) && depth == 1)
00247                 if (parse_keyframes_index(s, ioc, vstream ? vstream : astream,
00248                                           max_pos) < 0)
00249                     return -1;
00250 
00251             while(avio_tell(ioc) < max_pos - 2 && (keylen = avio_rb16(ioc))) {
00252                 avio_skip(ioc, keylen); //skip key string
00253                 if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
00254                     return -1; //if we couldn't skip, bomb out.
00255             }
00256             if(avio_r8(ioc) != AMF_END_OF_OBJECT)
00257                 return -1;
00258         }
00259             break;
00260         case AMF_DATA_TYPE_NULL:
00261         case AMF_DATA_TYPE_UNDEFINED:
00262         case AMF_DATA_TYPE_UNSUPPORTED:
00263             break; //these take up no additional space
00264         case AMF_DATA_TYPE_MIXEDARRAY:
00265             avio_skip(ioc, 4); //skip 32-bit max array index
00266             while(avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
00267                 //this is the only case in which we would want a nested parse to not skip over the object
00268                 if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
00269                     return -1;
00270             }
00271             if(avio_r8(ioc) != AMF_END_OF_OBJECT)
00272                 return -1;
00273             break;
00274         case AMF_DATA_TYPE_ARRAY: {
00275             unsigned int arraylen, i;
00276 
00277             arraylen = avio_rb32(ioc);
00278             for(i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
00279                 if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
00280                     return -1; //if we couldn't skip, bomb out.
00281             }
00282         }
00283             break;
00284         case AMF_DATA_TYPE_DATE:
00285             avio_skip(ioc, 8 + 2); //timestamp (double) and UTC offset (int16)
00286             break;
00287         default: //unsupported type, we couldn't skip
00288             return -1;
00289     }
00290 
00291     if(depth == 1 && key) { //only look for metadata values when we are not nested and key != NULL
00292         acodec = astream ? astream->codec : NULL;
00293         vcodec = vstream ? vstream->codec : NULL;
00294 
00295         if (amf_type == AMF_DATA_TYPE_NUMBER) {
00296             if (!strcmp(key, "duration"))
00297                 s->duration = num_val * AV_TIME_BASE;
00298             else if (!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
00299                 vcodec->bit_rate = num_val * 1024.0;
00300             else if (!strcmp(key, "audiodatarate") && acodec && 0 <= (int)(num_val * 1024.0))
00301                 acodec->bit_rate = num_val * 1024.0;
00302         }
00303 
00304         if (!strcmp(key, "duration")        ||
00305             !strcmp(key, "filesize")        ||
00306             !strcmp(key, "width")           ||
00307             !strcmp(key, "height")          ||
00308             !strcmp(key, "videodatarate")   ||
00309             !strcmp(key, "framerate")       ||
00310             !strcmp(key, "videocodecid")    ||
00311             !strcmp(key, "audiodatarate")   ||
00312             !strcmp(key, "audiosamplerate") ||
00313             !strcmp(key, "audiosamplesize") ||
00314             !strcmp(key, "stereo")          ||
00315             !strcmp(key, "audiocodecid"))
00316             return 0;
00317 
00318         if(amf_type == AMF_DATA_TYPE_BOOL) {
00319             av_strlcpy(str_val, num_val > 0 ? "true" : "false", sizeof(str_val));
00320             av_dict_set(&s->metadata, key, str_val, 0);
00321         } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
00322             snprintf(str_val, sizeof(str_val), "%.f", num_val);
00323             av_dict_set(&s->metadata, key, str_val, 0);
00324         } else if (amf_type == AMF_DATA_TYPE_STRING)
00325             av_dict_set(&s->metadata, key, str_val, 0);
00326     }
00327 
00328     return 0;
00329 }
00330 
00331 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
00332     AMFDataType type;
00333     AVStream *stream, *astream, *vstream;
00334     AVIOContext *ioc;
00335     int i;
00336     char buffer[11]; //only needs to hold the string "onMetaData". Anything longer is something we don't want.
00337 
00338     astream = NULL;
00339     vstream = NULL;
00340     ioc = s->pb;
00341 
00342     //first object needs to be "onMetaData" string
00343     type = avio_r8(ioc);
00344     if(type != AMF_DATA_TYPE_STRING || amf_get_string(ioc, buffer, sizeof(buffer)) < 0 || strcmp(buffer, "onMetaData"))
00345         return -1;
00346 
00347     //find the streams now so that amf_parse_object doesn't need to do the lookup every time it is called.
00348     for(i = 0; i < s->nb_streams; i++) {
00349         stream = s->streams[i];
00350         if     (stream->codec->codec_type == AVMEDIA_TYPE_AUDIO) astream = stream;
00351         else if(stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) vstream = stream;
00352     }
00353 
00354     //parse the second object (we want a mixed array)
00355     if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
00356         return -1;
00357 
00358     return 0;
00359 }
00360 
00361 static AVStream *create_stream(AVFormatContext *s, int is_audio){
00362     AVStream *st = avformat_new_stream(s, NULL);
00363     if (!st)
00364         return NULL;
00365     st->id = is_audio;
00366     st->codec->codec_type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
00367     avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
00368     return st;
00369 }
00370 
00371 static int flv_read_header(AVFormatContext *s,
00372                            AVFormatParameters *ap)
00373 {
00374     int offset, flags;
00375 
00376     avio_skip(s->pb, 4);
00377     flags = avio_r8(s->pb);
00378     /* old flvtool cleared this field */
00379     /* FIXME: better fix needed */
00380     if (!flags) {
00381         flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
00382         av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
00383     }
00384 
00385     if((flags & (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
00386              != (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
00387         s->ctx_flags |= AVFMTCTX_NOHEADER;
00388 
00389     if(flags & FLV_HEADER_FLAG_HASVIDEO){
00390         if(!create_stream(s, 0))
00391             return AVERROR(ENOMEM);
00392     }
00393     if(flags & FLV_HEADER_FLAG_HASAUDIO){
00394         if(!create_stream(s, 1))
00395             return AVERROR(ENOMEM);
00396     }
00397 
00398     offset = avio_rb32(s->pb);
00399     avio_seek(s->pb, offset, SEEK_SET);
00400     avio_skip(s->pb, 4);
00401 
00402     s->start_time = 0;
00403 
00404     return 0;
00405 }
00406 
00407 static int flv_read_close(AVFormatContext *s)
00408 {
00409     FLVContext *flv = s->priv_data;
00410     av_freep(&flv->new_extradata[0]);
00411     av_freep(&flv->new_extradata[1]);
00412     return 0;
00413 }
00414 
00415 static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
00416 {
00417     av_free(st->codec->extradata);
00418     st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
00419     if (!st->codec->extradata)
00420         return AVERROR(ENOMEM);
00421     st->codec->extradata_size = size;
00422     avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
00423     return 0;
00424 }
00425 
00426 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
00427                                int size)
00428 {
00429     av_free(flv->new_extradata[stream]);
00430     flv->new_extradata[stream] = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
00431     if (!flv->new_extradata[stream])
00432         return AVERROR(ENOMEM);
00433     flv->new_extradata_size[stream] = size;
00434     avio_read(pb, flv->new_extradata[stream], size);
00435     return 0;
00436 }
00437 
00438 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
00439 {
00440     FLVContext *flv = s->priv_data;
00441     int ret, i, type, size, flags, is_audio;
00442     int64_t next, pos;
00443     int64_t dts, pts = AV_NOPTS_VALUE;
00444     int sample_rate = 0, channels = 0;
00445     AVStream *st = NULL;
00446 
00447  for(;;avio_skip(s->pb, 4)){ /* pkt size is repeated at end. skip it */
00448     pos = avio_tell(s->pb);
00449     type = avio_r8(s->pb);
00450     size = avio_rb24(s->pb);
00451     dts = avio_rb24(s->pb);
00452     dts |= avio_r8(s->pb) << 24;
00453     av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
00454     if (s->pb->eof_reached)
00455         return AVERROR_EOF;
00456     avio_skip(s->pb, 3); /* stream id, always 0 */
00457     flags = 0;
00458 
00459     if(size == 0)
00460         continue;
00461 
00462     next= size + avio_tell(s->pb);
00463 
00464     if (type == FLV_TAG_TYPE_AUDIO) {
00465         is_audio=1;
00466         flags = avio_r8(s->pb);
00467         size--;
00468     } else if (type == FLV_TAG_TYPE_VIDEO) {
00469         is_audio=0;
00470         flags = avio_r8(s->pb);
00471         size--;
00472         if ((flags & 0xf0) == 0x50) /* video info / command frame */
00473             goto skip;
00474     } else {
00475         if (type == FLV_TAG_TYPE_META && size > 13+1+4)
00476             flv_read_metabody(s, next);
00477         else /* skip packet */
00478             av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
00479     skip:
00480         avio_seek(s->pb, next, SEEK_SET);
00481         continue;
00482     }
00483 
00484     /* skip empty data packets */
00485     if (!size)
00486         continue;
00487 
00488     /* now find stream */
00489     for(i=0;i<s->nb_streams;i++) {
00490         st = s->streams[i];
00491         if (st->id == is_audio)
00492             break;
00493     }
00494     if(i == s->nb_streams){
00495         av_log(s, AV_LOG_ERROR, "invalid stream\n");
00496         st= create_stream(s, is_audio);
00497         s->ctx_flags &= ~AVFMTCTX_NOHEADER;
00498     }
00499     av_dlog(s, "%d %X %d \n", is_audio, flags, st->discard);
00500     if(  (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||         is_audio))
00501        ||(st->discard >= AVDISCARD_BIDIR  &&  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
00502        || st->discard >= AVDISCARD_ALL
00503        ){
00504         avio_seek(s->pb, next, SEEK_SET);
00505         continue;
00506     }
00507     if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
00508         av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
00509     break;
00510  }
00511 
00512     // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps
00513     if(s->pb->seekable && (!s->duration || s->duration==AV_NOPTS_VALUE)){
00514         int size;
00515         const int64_t pos= avio_tell(s->pb);
00516         const int64_t fsize= avio_size(s->pb);
00517         avio_seek(s->pb, fsize-4, SEEK_SET);
00518         size= avio_rb32(s->pb);
00519         avio_seek(s->pb, fsize-3-size, SEEK_SET);
00520         if(size == avio_rb24(s->pb) + 11){
00521             uint32_t ts = avio_rb24(s->pb);
00522             ts |= avio_r8(s->pb) << 24;
00523             s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
00524         }
00525         avio_seek(s->pb, pos, SEEK_SET);
00526     }
00527 
00528     if(is_audio){
00529         int bits_per_coded_sample;
00530         channels    = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
00531         sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
00532         bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
00533         if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
00534             st->codec->channels              = channels;
00535             st->codec->sample_rate           = sample_rate;
00536             st->codec->bits_per_coded_sample = bits_per_coded_sample;
00537         }
00538         if(!st->codec->codec_id){
00539             flv_set_audio_codec(s, st, st->codec, flags & FLV_AUDIO_CODECID_MASK);
00540             flv->last_sample_rate = st->codec->sample_rate;
00541             flv->last_channels    = st->codec->channels;
00542         } else {
00543             AVCodecContext ctx;
00544             ctx.sample_rate = sample_rate;
00545             flv_set_audio_codec(s, st, &ctx, flags & FLV_AUDIO_CODECID_MASK);
00546             sample_rate = ctx.sample_rate;
00547         }
00548     }else{
00549         size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
00550     }
00551 
00552     if (st->codec->codec_id == CODEC_ID_AAC ||
00553         st->codec->codec_id == CODEC_ID_H264) {
00554         int type = avio_r8(s->pb);
00555         size--;
00556         if (st->codec->codec_id == CODEC_ID_H264) {
00557             int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000; // sign extension
00558             pts = dts + cts;
00559             if (cts < 0) { // dts are wrong
00560                 flv->wrong_dts = 1;
00561                 av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
00562             }
00563             if (flv->wrong_dts)
00564                 dts = AV_NOPTS_VALUE;
00565         }
00566         if (type == 0) {
00567             if (st->codec->extradata) {
00568                 if ((ret = flv_queue_extradata(flv, s->pb, is_audio, size)) < 0)
00569                     return ret;
00570                 ret = AVERROR(EAGAIN);
00571                 goto leave;
00572             }
00573             if ((ret = flv_get_extradata(s, st, size)) < 0)
00574                 return ret;
00575             if (st->codec->codec_id == CODEC_ID_AAC) {
00576                 MPEG4AudioConfig cfg;
00577                 avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
00578                                              st->codec->extradata_size * 8, 1);
00579                 st->codec->channels = cfg.channels;
00580                 if (cfg.ext_sample_rate)
00581                     st->codec->sample_rate = cfg.ext_sample_rate;
00582                 else
00583                     st->codec->sample_rate = cfg.sample_rate;
00584                 av_dlog(s, "mp4a config channels %d sample rate %d\n",
00585                         st->codec->channels, st->codec->sample_rate);
00586             }
00587 
00588             ret = AVERROR(EAGAIN);
00589             goto leave;
00590         }
00591     }
00592 
00593     /* skip empty data packets */
00594     if (!size) {
00595         ret = AVERROR(EAGAIN);
00596         goto leave;
00597     }
00598 
00599     ret= av_get_packet(s->pb, pkt, size);
00600     if (ret < 0) {
00601         return AVERROR(EIO);
00602     }
00603     /* note: we need to modify the packet size here to handle the last
00604        packet */
00605     pkt->size = ret;
00606     pkt->dts = dts;
00607     pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
00608     pkt->stream_index = st->index;
00609     if (flv->new_extradata[is_audio]) {
00610         uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
00611                                                 flv->new_extradata_size[is_audio]);
00612         if (side) {
00613             memcpy(side, flv->new_extradata[is_audio],
00614                    flv->new_extradata_size[is_audio]);
00615             av_freep(&flv->new_extradata[is_audio]);
00616             flv->new_extradata_size[is_audio] = 0;
00617         }
00618     }
00619     if (is_audio && (sample_rate != flv->last_sample_rate ||
00620                      channels != flv->last_channels)) {
00621         flv->last_sample_rate = sample_rate;
00622         flv->last_channels    = channels;
00623         ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
00624     }
00625 
00626     if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
00627         pkt->flags |= AV_PKT_FLAG_KEY;
00628 
00629 leave:
00630     avio_skip(s->pb, 4);
00631     return ret;
00632 }
00633 
00634 static int flv_read_seek(AVFormatContext *s, int stream_index,
00635     int64_t ts, int flags)
00636 {
00637     return avio_seek_time(s->pb, stream_index, ts, flags);
00638 }
00639 
00640 #if 0 /* don't know enough to implement this */
00641 static int flv_read_seek2(AVFormatContext *s, int stream_index,
00642     int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
00643 {
00644     int ret = AVERROR(ENOSYS);
00645 
00646     if (ts - min_ts > (uint64_t)(max_ts - ts)) flags |= AVSEEK_FLAG_BACKWARD;
00647 
00648     if (!s->pb->seekable) {
00649         if (stream_index < 0) {
00650             stream_index = av_find_default_stream_index(s);
00651             if (stream_index < 0)
00652                 return -1;
00653 
00654             /* timestamp for default must be expressed in AV_TIME_BASE units */
00655             ts = av_rescale_rnd(ts, 1000, AV_TIME_BASE,
00656                 flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
00657         }
00658         ret = avio_seek_time(s->pb, stream_index, ts, flags);
00659     }
00660 
00661     if (ret == AVERROR(ENOSYS))
00662         ret = av_seek_frame(s, stream_index, ts, flags);
00663     return ret;
00664 }
00665 #endif
00666 
00667 AVInputFormat ff_flv_demuxer = {
00668     .name           = "flv",
00669     .long_name      = NULL_IF_CONFIG_SMALL("FLV format"),
00670     .priv_data_size = sizeof(FLVContext),
00671     .read_probe     = flv_probe,
00672     .read_header    = flv_read_header,
00673     .read_packet    = flv_read_packet,
00674     .read_seek = flv_read_seek,
00675 #if 0
00676     .read_seek2 = flv_read_seek2,
00677 #endif
00678     .read_close = flv_read_close,
00679     .extensions = "flv",
00680     .value = CODEC_ID_FLV1,
00681 };
Generated on Sun Apr 22 2012 21:54:00 for Libav by doxygen 1.7.1