You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

624 lines
22KB

  1. /*
  2. * FLV demuxer
  3. * Copyright (c) 2003 The Libav Project
  4. *
  5. * This demuxer will generate a 1 byte extradata for VP6F content.
  6. * It is composed of:
  7. * - upper 4bits: difference between encoded width and visible width
  8. * - lower 4bits: difference between encoded height and visible height
  9. *
  10. * This file is part of Libav.
  11. *
  12. * Libav is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * Libav is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with Libav; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/dict.h"
  28. #include "libavutil/intfloat_readwrite.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavcodec/bytestream.h"
  31. #include "libavcodec/mpeg4audio.h"
  32. #include "avformat.h"
  33. #include "internal.h"
  34. #include "avio_internal.h"
  35. #include "flv.h"
  36. #define KEYFRAMES_TAG "keyframes"
  37. #define KEYFRAMES_TIMESTAMP_TAG "times"
  38. #define KEYFRAMES_BYTEOFFSET_TAG "filepositions"
  39. typedef struct {
  40. int wrong_dts; ///< wrong dts due to negative cts
  41. } FLVContext;
  42. static int flv_probe(AVProbeData *p)
  43. {
  44. const uint8_t *d;
  45. d = p->buf;
  46. if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
  47. return AVPROBE_SCORE_MAX;
  48. }
  49. return 0;
  50. }
  51. static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, int flv_codecid) {
  52. AVCodecContext *acodec = astream->codec;
  53. switch(flv_codecid) {
  54. //no distinction between S16 and S8 PCM codec flags
  55. case FLV_CODECID_PCM:
  56. acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 :
  57. #if HAVE_BIGENDIAN
  58. CODEC_ID_PCM_S16BE;
  59. #else
  60. CODEC_ID_PCM_S16LE;
  61. #endif
  62. break;
  63. case FLV_CODECID_PCM_LE:
  64. acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 : CODEC_ID_PCM_S16LE; break;
  65. case FLV_CODECID_AAC : acodec->codec_id = CODEC_ID_AAC; break;
  66. case FLV_CODECID_ADPCM: acodec->codec_id = CODEC_ID_ADPCM_SWF; break;
  67. case FLV_CODECID_SPEEX:
  68. acodec->codec_id = CODEC_ID_SPEEX;
  69. acodec->sample_rate = 16000;
  70. break;
  71. case FLV_CODECID_MP3 : acodec->codec_id = CODEC_ID_MP3 ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
  72. case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
  73. acodec->sample_rate = 8000; //in case metadata does not otherwise declare samplerate
  74. acodec->codec_id = CODEC_ID_NELLYMOSER;
  75. break;
  76. case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
  77. acodec->sample_rate = 16000;
  78. acodec->codec_id = CODEC_ID_NELLYMOSER;
  79. break;
  80. case FLV_CODECID_NELLYMOSER:
  81. acodec->codec_id = CODEC_ID_NELLYMOSER;
  82. break;
  83. default:
  84. av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
  85. acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
  86. }
  87. }
  88. static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
  89. AVCodecContext *vcodec = vstream->codec;
  90. switch(flv_codecid) {
  91. case FLV_CODECID_H263 : vcodec->codec_id = CODEC_ID_FLV1 ; break;
  92. case FLV_CODECID_SCREEN: vcodec->codec_id = CODEC_ID_FLASHSV; break;
  93. case FLV_CODECID_SCREEN2: vcodec->codec_id = CODEC_ID_FLASHSV2; break;
  94. case FLV_CODECID_VP6 : vcodec->codec_id = CODEC_ID_VP6F ;
  95. case FLV_CODECID_VP6A :
  96. if(flv_codecid == FLV_CODECID_VP6A)
  97. vcodec->codec_id = CODEC_ID_VP6A;
  98. if(vcodec->extradata_size != 1) {
  99. vcodec->extradata_size = 1;
  100. vcodec->extradata = av_malloc(1);
  101. }
  102. vcodec->extradata[0] = avio_r8(s->pb);
  103. return 1; // 1 byte body size adjustment for flv_read_packet()
  104. case FLV_CODECID_H264:
  105. vcodec->codec_id = CODEC_ID_H264;
  106. return 3; // not 4, reading packet type will consume one byte
  107. default:
  108. av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
  109. vcodec->codec_tag = flv_codecid;
  110. }
  111. return 0;
  112. }
  113. static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize) {
  114. int length = avio_rb16(ioc);
  115. if(length >= buffsize) {
  116. avio_skip(ioc, length);
  117. return -1;
  118. }
  119. avio_read(ioc, buffer, length);
  120. buffer[length] = '\0';
  121. return length;
  122. }
  123. static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, AVStream *vstream, int64_t max_pos) {
  124. unsigned int arraylen = 0, timeslen = 0, fileposlen = 0, i;
  125. double num_val;
  126. char str_val[256];
  127. int64_t *times = NULL;
  128. int64_t *filepositions = NULL;
  129. int ret = AVERROR(ENOSYS);
  130. int64_t initial_pos = avio_tell(ioc);
  131. AVDictionaryEntry *creator = av_dict_get(s->metadata, "metadatacreator",
  132. NULL, 0);
  133. if (creator && !strcmp(creator->value, "MEGA")) {
  134. /* Files with this metadatacreator tag seem to have filepositions
  135. * pointing at the 4 trailer bytes of the previous packet,
  136. * which isn't the norm (nor what we expect here, nor what
  137. * jwplayer + lighttpd expect, nor what flvtool2 produces).
  138. * Just ignore the index in this case, instead of risking trying
  139. * to adjust it to something that might or might not work. */
  140. return 0;
  141. }
  142. while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
  143. int64_t* current_array;
  144. // Expect array object in context
  145. if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
  146. break;
  147. arraylen = avio_rb32(ioc);
  148. if (arraylen >> 28)
  149. break;
  150. /*
  151. * Expect only 'times' or 'filepositions' sub-arrays in other case refuse to use such metadata
  152. * for indexing
  153. */
  154. if (!strcmp(KEYFRAMES_TIMESTAMP_TAG, str_val) && !times) {
  155. if (!(times = av_mallocz(sizeof(*times) * arraylen))) {
  156. ret = AVERROR(ENOMEM);
  157. goto finish;
  158. }
  159. timeslen = arraylen;
  160. current_array = times;
  161. } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) && !filepositions) {
  162. if (!(filepositions = av_mallocz(sizeof(*filepositions) * arraylen))) {
  163. ret = AVERROR(ENOMEM);
  164. goto finish;
  165. }
  166. fileposlen = arraylen;
  167. current_array = filepositions;
  168. } else // unexpected metatag inside keyframes, will not use such metadata for indexing
  169. break;
  170. for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
  171. if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
  172. goto finish;
  173. num_val = av_int2dbl(avio_rb64(ioc));
  174. current_array[i] = num_val;
  175. }
  176. if (times && filepositions) {
  177. // All done, exiting at a position allowing amf_parse_object
  178. // to finish parsing the object
  179. ret = 0;
  180. break;
  181. }
  182. }
  183. if (!ret && timeslen == fileposlen)
  184. for (i = 0; i < fileposlen; i++)
  185. av_add_index_entry(vstream, filepositions[i], times[i]*1000, 0, 0, AVINDEX_KEYFRAME);
  186. else
  187. av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
  188. finish:
  189. av_freep(&times);
  190. av_freep(&filepositions);
  191. // If we got unexpected data, but successfully reset back to
  192. // the start pos, the caller can continue parsing
  193. if (ret < 0 && avio_seek(ioc, initial_pos, SEEK_SET) > 0)
  194. return 0;
  195. return ret;
  196. }
  197. static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
  198. AVCodecContext *acodec, *vcodec;
  199. AVIOContext *ioc;
  200. AMFDataType amf_type;
  201. char str_val[256];
  202. double num_val;
  203. num_val = 0;
  204. ioc = s->pb;
  205. amf_type = avio_r8(ioc);
  206. switch(amf_type) {
  207. case AMF_DATA_TYPE_NUMBER:
  208. num_val = av_int2dbl(avio_rb64(ioc)); break;
  209. case AMF_DATA_TYPE_BOOL:
  210. num_val = avio_r8(ioc); break;
  211. case AMF_DATA_TYPE_STRING:
  212. if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
  213. return -1;
  214. break;
  215. case AMF_DATA_TYPE_OBJECT: {
  216. unsigned int keylen;
  217. if ((vstream || astream) && key && !strcmp(KEYFRAMES_TAG, key) && depth == 1)
  218. if (parse_keyframes_index(s, ioc, vstream ? vstream : astream,
  219. max_pos) < 0)
  220. return -1;
  221. while(avio_tell(ioc) < max_pos - 2 && (keylen = avio_rb16(ioc))) {
  222. avio_skip(ioc, keylen); //skip key string
  223. if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
  224. return -1; //if we couldn't skip, bomb out.
  225. }
  226. if(avio_r8(ioc) != AMF_END_OF_OBJECT)
  227. return -1;
  228. }
  229. break;
  230. case AMF_DATA_TYPE_NULL:
  231. case AMF_DATA_TYPE_UNDEFINED:
  232. case AMF_DATA_TYPE_UNSUPPORTED:
  233. break; //these take up no additional space
  234. case AMF_DATA_TYPE_MIXEDARRAY:
  235. avio_skip(ioc, 4); //skip 32-bit max array index
  236. while(avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
  237. //this is the only case in which we would want a nested parse to not skip over the object
  238. if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
  239. return -1;
  240. }
  241. if(avio_r8(ioc) != AMF_END_OF_OBJECT)
  242. return -1;
  243. break;
  244. case AMF_DATA_TYPE_ARRAY: {
  245. unsigned int arraylen, i;
  246. arraylen = avio_rb32(ioc);
  247. for(i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
  248. if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
  249. return -1; //if we couldn't skip, bomb out.
  250. }
  251. }
  252. break;
  253. case AMF_DATA_TYPE_DATE:
  254. avio_skip(ioc, 8 + 2); //timestamp (double) and UTC offset (int16)
  255. break;
  256. default: //unsupported type, we couldn't skip
  257. return -1;
  258. }
  259. if(depth == 1 && key) { //only look for metadata values when we are not nested and key != NULL
  260. acodec = astream ? astream->codec : NULL;
  261. vcodec = vstream ? vstream->codec : NULL;
  262. if (amf_type == AMF_DATA_TYPE_NUMBER) {
  263. if (!strcmp(key, "duration"))
  264. s->duration = num_val * AV_TIME_BASE;
  265. else if (!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
  266. vcodec->bit_rate = num_val * 1024.0;
  267. else if (!strcmp(key, "audiodatarate") && acodec && 0 <= (int)(num_val * 1024.0))
  268. acodec->bit_rate = num_val * 1024.0;
  269. }
  270. if (!strcmp(key, "duration") ||
  271. !strcmp(key, "filesize") ||
  272. !strcmp(key, "width") ||
  273. !strcmp(key, "height") ||
  274. !strcmp(key, "videodatarate") ||
  275. !strcmp(key, "framerate") ||
  276. !strcmp(key, "videocodecid") ||
  277. !strcmp(key, "audiodatarate") ||
  278. !strcmp(key, "audiosamplerate") ||
  279. !strcmp(key, "audiosamplesize") ||
  280. !strcmp(key, "stereo") ||
  281. !strcmp(key, "audiocodecid"))
  282. return 0;
  283. if(amf_type == AMF_DATA_TYPE_BOOL) {
  284. av_strlcpy(str_val, num_val > 0 ? "true" : "false", sizeof(str_val));
  285. av_dict_set(&s->metadata, key, str_val, 0);
  286. } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
  287. snprintf(str_val, sizeof(str_val), "%.f", num_val);
  288. av_dict_set(&s->metadata, key, str_val, 0);
  289. } else if (amf_type == AMF_DATA_TYPE_STRING)
  290. av_dict_set(&s->metadata, key, str_val, 0);
  291. }
  292. return 0;
  293. }
  294. static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
  295. AMFDataType type;
  296. AVStream *stream, *astream, *vstream;
  297. AVIOContext *ioc;
  298. int i;
  299. char buffer[11]; //only needs to hold the string "onMetaData". Anything longer is something we don't want.
  300. astream = NULL;
  301. vstream = NULL;
  302. ioc = s->pb;
  303. //first object needs to be "onMetaData" string
  304. type = avio_r8(ioc);
  305. if(type != AMF_DATA_TYPE_STRING || amf_get_string(ioc, buffer, sizeof(buffer)) < 0 || strcmp(buffer, "onMetaData"))
  306. return -1;
  307. //find the streams now so that amf_parse_object doesn't need to do the lookup every time it is called.
  308. for(i = 0; i < s->nb_streams; i++) {
  309. stream = s->streams[i];
  310. if (stream->codec->codec_type == AVMEDIA_TYPE_AUDIO) astream = stream;
  311. else if(stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) vstream = stream;
  312. }
  313. //parse the second object (we want a mixed array)
  314. if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
  315. return -1;
  316. return 0;
  317. }
  318. static AVStream *create_stream(AVFormatContext *s, int is_audio){
  319. AVStream *st = avformat_new_stream(s, NULL);
  320. if (!st)
  321. return NULL;
  322. st->id = is_audio;
  323. st->codec->codec_type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
  324. avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
  325. return st;
  326. }
  327. static int flv_read_header(AVFormatContext *s,
  328. AVFormatParameters *ap)
  329. {
  330. int offset, flags;
  331. avio_skip(s->pb, 4);
  332. flags = avio_r8(s->pb);
  333. /* old flvtool cleared this field */
  334. /* FIXME: better fix needed */
  335. if (!flags) {
  336. flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
  337. av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
  338. }
  339. if((flags & (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
  340. != (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
  341. s->ctx_flags |= AVFMTCTX_NOHEADER;
  342. if(flags & FLV_HEADER_FLAG_HASVIDEO){
  343. if(!create_stream(s, 0))
  344. return AVERROR(ENOMEM);
  345. }
  346. if(flags & FLV_HEADER_FLAG_HASAUDIO){
  347. if(!create_stream(s, 1))
  348. return AVERROR(ENOMEM);
  349. }
  350. offset = avio_rb32(s->pb);
  351. avio_seek(s->pb, offset, SEEK_SET);
  352. avio_skip(s->pb, 4);
  353. s->start_time = 0;
  354. return 0;
  355. }
  356. static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
  357. {
  358. av_free(st->codec->extradata);
  359. st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  360. if (!st->codec->extradata)
  361. return AVERROR(ENOMEM);
  362. st->codec->extradata_size = size;
  363. avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
  364. return 0;
  365. }
  366. static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
  367. {
  368. FLVContext *flv = s->priv_data;
  369. int ret, i, type, size, flags, is_audio;
  370. int64_t next, pos;
  371. int64_t dts, pts = AV_NOPTS_VALUE;
  372. AVStream *st = NULL;
  373. for(;;avio_skip(s->pb, 4)){ /* pkt size is repeated at end. skip it */
  374. pos = avio_tell(s->pb);
  375. type = avio_r8(s->pb);
  376. size = avio_rb24(s->pb);
  377. dts = avio_rb24(s->pb);
  378. dts |= avio_r8(s->pb) << 24;
  379. av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
  380. if (s->pb->eof_reached)
  381. return AVERROR_EOF;
  382. avio_skip(s->pb, 3); /* stream id, always 0 */
  383. flags = 0;
  384. if(size == 0)
  385. continue;
  386. next= size + avio_tell(s->pb);
  387. if (type == FLV_TAG_TYPE_AUDIO) {
  388. is_audio=1;
  389. flags = avio_r8(s->pb);
  390. size--;
  391. } else if (type == FLV_TAG_TYPE_VIDEO) {
  392. is_audio=0;
  393. flags = avio_r8(s->pb);
  394. size--;
  395. if ((flags & 0xf0) == 0x50) /* video info / command frame */
  396. goto skip;
  397. } else {
  398. if (type == FLV_TAG_TYPE_META && size > 13+1+4)
  399. flv_read_metabody(s, next);
  400. else /* skip packet */
  401. av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
  402. skip:
  403. avio_seek(s->pb, next, SEEK_SET);
  404. continue;
  405. }
  406. /* skip empty data packets */
  407. if (!size)
  408. continue;
  409. /* now find stream */
  410. for(i=0;i<s->nb_streams;i++) {
  411. st = s->streams[i];
  412. if (st->id == is_audio)
  413. break;
  414. }
  415. if(i == s->nb_streams){
  416. av_log(s, AV_LOG_ERROR, "invalid stream\n");
  417. st= create_stream(s, is_audio);
  418. s->ctx_flags &= ~AVFMTCTX_NOHEADER;
  419. }
  420. av_dlog(s, "%d %X %d \n", is_audio, flags, st->discard);
  421. if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio))
  422. ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
  423. || st->discard >= AVDISCARD_ALL
  424. ){
  425. avio_seek(s->pb, next, SEEK_SET);
  426. continue;
  427. }
  428. if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
  429. av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
  430. break;
  431. }
  432. // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps
  433. if(s->pb->seekable && (!s->duration || s->duration==AV_NOPTS_VALUE)){
  434. int size;
  435. const int64_t pos= avio_tell(s->pb);
  436. const int64_t fsize= avio_size(s->pb);
  437. avio_seek(s->pb, fsize-4, SEEK_SET);
  438. size= avio_rb32(s->pb);
  439. avio_seek(s->pb, fsize-3-size, SEEK_SET);
  440. if(size == avio_rb24(s->pb) + 11){
  441. uint32_t ts = avio_rb24(s->pb);
  442. ts |= avio_r8(s->pb) << 24;
  443. s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
  444. }
  445. avio_seek(s->pb, pos, SEEK_SET);
  446. }
  447. if(is_audio){
  448. if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
  449. st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
  450. st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
  451. st->codec->bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
  452. }
  453. if(!st->codec->codec_id){
  454. flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);
  455. }
  456. }else{
  457. size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
  458. }
  459. if (st->codec->codec_id == CODEC_ID_AAC ||
  460. st->codec->codec_id == CODEC_ID_H264) {
  461. int type = avio_r8(s->pb);
  462. size--;
  463. if (st->codec->codec_id == CODEC_ID_H264) {
  464. int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000; // sign extension
  465. pts = dts + cts;
  466. if (cts < 0) { // dts are wrong
  467. flv->wrong_dts = 1;
  468. av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
  469. }
  470. if (flv->wrong_dts)
  471. dts = AV_NOPTS_VALUE;
  472. }
  473. if (type == 0) {
  474. if ((ret = flv_get_extradata(s, st, size)) < 0)
  475. return ret;
  476. if (st->codec->codec_id == CODEC_ID_AAC) {
  477. MPEG4AudioConfig cfg;
  478. avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
  479. st->codec->extradata_size * 8, 1);
  480. st->codec->channels = cfg.channels;
  481. if (cfg.ext_sample_rate)
  482. st->codec->sample_rate = cfg.ext_sample_rate;
  483. else
  484. st->codec->sample_rate = cfg.sample_rate;
  485. av_dlog(s, "mp4a config channels %d sample rate %d\n",
  486. st->codec->channels, st->codec->sample_rate);
  487. }
  488. ret = AVERROR(EAGAIN);
  489. goto leave;
  490. }
  491. }
  492. /* skip empty data packets */
  493. if (!size) {
  494. ret = AVERROR(EAGAIN);
  495. goto leave;
  496. }
  497. ret= av_get_packet(s->pb, pkt, size);
  498. if (ret < 0) {
  499. return AVERROR(EIO);
  500. }
  501. /* note: we need to modify the packet size here to handle the last
  502. packet */
  503. pkt->size = ret;
  504. pkt->dts = dts;
  505. pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
  506. pkt->stream_index = st->index;
  507. if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
  508. pkt->flags |= AV_PKT_FLAG_KEY;
  509. leave:
  510. avio_skip(s->pb, 4);
  511. return ret;
  512. }
  513. static int flv_read_seek(AVFormatContext *s, int stream_index,
  514. int64_t ts, int flags)
  515. {
  516. return avio_seek_time(s->pb, stream_index, ts, flags);
  517. }
  518. #if 0 /* don't know enough to implement this */
  519. static int flv_read_seek2(AVFormatContext *s, int stream_index,
  520. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  521. {
  522. int ret = AVERROR(ENOSYS);
  523. if (ts - min_ts > (uint64_t)(max_ts - ts)) flags |= AVSEEK_FLAG_BACKWARD;
  524. if (!s->pb->seekable) {
  525. if (stream_index < 0) {
  526. stream_index = av_find_default_stream_index(s);
  527. if (stream_index < 0)
  528. return -1;
  529. /* timestamp for default must be expressed in AV_TIME_BASE units */
  530. ts = av_rescale_rnd(ts, 1000, AV_TIME_BASE,
  531. flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
  532. }
  533. ret = avio_seek_time(s->pb, stream_index, ts, flags);
  534. }
  535. if (ret == AVERROR(ENOSYS))
  536. ret = av_seek_frame(s, stream_index, ts, flags);
  537. return ret;
  538. }
  539. #endif
  540. AVInputFormat ff_flv_demuxer = {
  541. .name = "flv",
  542. .long_name = NULL_IF_CONFIG_SMALL("FLV format"),
  543. .priv_data_size = sizeof(FLVContext),
  544. .read_probe = flv_probe,
  545. .read_header = flv_read_header,
  546. .read_packet = flv_read_packet,
  547. .read_seek = flv_read_seek,
  548. #if 0
  549. .read_seek2 = flv_read_seek2,
  550. #endif
  551. .extensions = "flv",
  552. .value = CODEC_ID_FLV1,
  553. };