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.

717 lines
25KB

  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.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. #define VALIDATE_INDEX_TS_THRESH 2500
  40. typedef struct {
  41. int wrong_dts; ///< wrong dts due to negative cts
  42. uint8_t *new_extradata[2];
  43. int new_extradata_size[2];
  44. int last_sample_rate;
  45. int last_channels;
  46. struct {
  47. int64_t dts;
  48. int64_t pos;
  49. } validate_index[2];
  50. int validate_next;
  51. int validate_count;
  52. } FLVContext;
  53. static int flv_probe(AVProbeData *p)
  54. {
  55. const uint8_t *d;
  56. d = p->buf;
  57. if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
  58. return AVPROBE_SCORE_MAX;
  59. }
  60. return 0;
  61. }
  62. static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, AVCodecContext *acodec, int flv_codecid) {
  63. switch(flv_codecid) {
  64. //no distinction between S16 and S8 PCM codec flags
  65. case FLV_CODECID_PCM:
  66. acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 :
  67. #if HAVE_BIGENDIAN
  68. CODEC_ID_PCM_S16BE;
  69. #else
  70. CODEC_ID_PCM_S16LE;
  71. #endif
  72. break;
  73. case FLV_CODECID_PCM_LE:
  74. acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 : CODEC_ID_PCM_S16LE; break;
  75. case FLV_CODECID_AAC : acodec->codec_id = CODEC_ID_AAC; break;
  76. case FLV_CODECID_ADPCM: acodec->codec_id = CODEC_ID_ADPCM_SWF; break;
  77. case FLV_CODECID_SPEEX:
  78. acodec->codec_id = CODEC_ID_SPEEX;
  79. acodec->sample_rate = 16000;
  80. break;
  81. case FLV_CODECID_MP3 : acodec->codec_id = CODEC_ID_MP3 ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
  82. case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
  83. acodec->sample_rate = 8000; //in case metadata does not otherwise declare samplerate
  84. acodec->codec_id = CODEC_ID_NELLYMOSER;
  85. break;
  86. case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
  87. acodec->sample_rate = 16000;
  88. acodec->codec_id = CODEC_ID_NELLYMOSER;
  89. break;
  90. case FLV_CODECID_NELLYMOSER:
  91. acodec->codec_id = CODEC_ID_NELLYMOSER;
  92. break;
  93. default:
  94. av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
  95. acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
  96. }
  97. }
  98. static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
  99. AVCodecContext *vcodec = vstream->codec;
  100. switch(flv_codecid) {
  101. case FLV_CODECID_H263 : vcodec->codec_id = CODEC_ID_FLV1 ; break;
  102. case FLV_CODECID_SCREEN: vcodec->codec_id = CODEC_ID_FLASHSV; break;
  103. case FLV_CODECID_SCREEN2: vcodec->codec_id = CODEC_ID_FLASHSV2; break;
  104. case FLV_CODECID_VP6 : vcodec->codec_id = CODEC_ID_VP6F ;
  105. case FLV_CODECID_VP6A :
  106. if(flv_codecid == FLV_CODECID_VP6A)
  107. vcodec->codec_id = CODEC_ID_VP6A;
  108. if(vcodec->extradata_size != 1) {
  109. vcodec->extradata_size = 1;
  110. vcodec->extradata = av_malloc(1);
  111. }
  112. vcodec->extradata[0] = avio_r8(s->pb);
  113. return 1; // 1 byte body size adjustment for flv_read_packet()
  114. case FLV_CODECID_H264:
  115. vcodec->codec_id = CODEC_ID_H264;
  116. return 3; // not 4, reading packet type will consume one byte
  117. default:
  118. av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
  119. vcodec->codec_tag = flv_codecid;
  120. }
  121. return 0;
  122. }
  123. static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize) {
  124. int length = avio_rb16(ioc);
  125. if(length >= buffsize) {
  126. avio_skip(ioc, length);
  127. return -1;
  128. }
  129. avio_read(ioc, buffer, length);
  130. buffer[length] = '\0';
  131. return length;
  132. }
  133. static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, AVStream *vstream, int64_t max_pos) {
  134. FLVContext *flv = s->priv_data;
  135. unsigned int arraylen = 0, timeslen = 0, fileposlen = 0, i;
  136. double num_val;
  137. char str_val[256];
  138. int64_t *times = NULL;
  139. int64_t *filepositions = NULL;
  140. int ret = AVERROR(ENOSYS);
  141. int64_t initial_pos = avio_tell(ioc);
  142. if (s->flags & AVFMT_FLAG_IGNIDX)
  143. return 0;
  144. while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
  145. int64_t* current_array;
  146. // Expect array object in context
  147. if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
  148. break;
  149. arraylen = avio_rb32(ioc);
  150. if (arraylen >> 28)
  151. break;
  152. /*
  153. * Expect only 'times' or 'filepositions' sub-arrays in other case refuse to use such metadata
  154. * for indexing
  155. */
  156. if (!strcmp(KEYFRAMES_TIMESTAMP_TAG, str_val) && !times) {
  157. if (!(times = av_mallocz(sizeof(*times) * arraylen))) {
  158. ret = AVERROR(ENOMEM);
  159. goto finish;
  160. }
  161. timeslen = arraylen;
  162. current_array = times;
  163. } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) && !filepositions) {
  164. if (!(filepositions = av_mallocz(sizeof(*filepositions) * arraylen))) {
  165. ret = AVERROR(ENOMEM);
  166. goto finish;
  167. }
  168. fileposlen = arraylen;
  169. current_array = filepositions;
  170. } else // unexpected metatag inside keyframes, will not use such metadata for indexing
  171. break;
  172. for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
  173. if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
  174. goto finish;
  175. num_val = av_int2double(avio_rb64(ioc));
  176. current_array[i] = num_val;
  177. }
  178. if (times && filepositions) {
  179. // All done, exiting at a position allowing amf_parse_object
  180. // to finish parsing the object
  181. ret = 0;
  182. break;
  183. }
  184. }
  185. if (!ret && timeslen == fileposlen) {
  186. for (i = 0; i < fileposlen; i++) {
  187. av_add_index_entry(vstream, filepositions[i], times[i]*1000,
  188. 0, 0, AVINDEX_KEYFRAME);
  189. if (i < 2) {
  190. flv->validate_index[i].pos = filepositions[i];
  191. flv->validate_index[i].dts = times[i] * 1000;
  192. flv->validate_count = i + 1;
  193. }
  194. }
  195. } else
  196. av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
  197. finish:
  198. av_freep(&times);
  199. av_freep(&filepositions);
  200. // If we got unexpected data, but successfully reset back to
  201. // the start pos, the caller can continue parsing
  202. if (ret < 0 && avio_seek(ioc, initial_pos, SEEK_SET) > 0)
  203. return 0;
  204. return ret;
  205. }
  206. static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
  207. AVCodecContext *acodec, *vcodec;
  208. AVIOContext *ioc;
  209. AMFDataType amf_type;
  210. char str_val[256];
  211. double num_val;
  212. num_val = 0;
  213. ioc = s->pb;
  214. amf_type = avio_r8(ioc);
  215. switch(amf_type) {
  216. case AMF_DATA_TYPE_NUMBER:
  217. num_val = av_int2double(avio_rb64(ioc)); break;
  218. case AMF_DATA_TYPE_BOOL:
  219. num_val = avio_r8(ioc); break;
  220. case AMF_DATA_TYPE_STRING:
  221. if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
  222. return -1;
  223. break;
  224. case AMF_DATA_TYPE_OBJECT:
  225. if ((vstream || astream) && key && !strcmp(KEYFRAMES_TAG, key) && depth == 1)
  226. if (parse_keyframes_index(s, ioc, vstream ? vstream : astream,
  227. max_pos) < 0)
  228. return -1;
  229. while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
  230. if (amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
  231. return -1; //if we couldn't skip, bomb out.
  232. }
  233. if(avio_r8(ioc) != AMF_END_OF_OBJECT)
  234. return -1;
  235. break;
  236. case AMF_DATA_TYPE_NULL:
  237. case AMF_DATA_TYPE_UNDEFINED:
  238. case AMF_DATA_TYPE_UNSUPPORTED:
  239. break; //these take up no additional space
  240. case AMF_DATA_TYPE_MIXEDARRAY:
  241. avio_skip(ioc, 4); //skip 32-bit max array index
  242. while(avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
  243. //this is the only case in which we would want a nested parse to not skip over the object
  244. if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
  245. return -1;
  246. }
  247. if(avio_r8(ioc) != AMF_END_OF_OBJECT)
  248. return -1;
  249. break;
  250. case AMF_DATA_TYPE_ARRAY: {
  251. unsigned int arraylen, i;
  252. arraylen = avio_rb32(ioc);
  253. for(i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
  254. if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
  255. return -1; //if we couldn't skip, bomb out.
  256. }
  257. }
  258. break;
  259. case AMF_DATA_TYPE_DATE:
  260. avio_skip(ioc, 8 + 2); //timestamp (double) and UTC offset (int16)
  261. break;
  262. default: //unsupported type, we couldn't skip
  263. return -1;
  264. }
  265. if(depth == 1 && key) { //only look for metadata values when we are not nested and key != NULL
  266. acodec = astream ? astream->codec : NULL;
  267. vcodec = vstream ? vstream->codec : NULL;
  268. if (amf_type == AMF_DATA_TYPE_NUMBER) {
  269. if (!strcmp(key, "duration"))
  270. s->duration = num_val * AV_TIME_BASE;
  271. else if (!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
  272. vcodec->bit_rate = num_val * 1024.0;
  273. else if (!strcmp(key, "audiodatarate") && acodec && 0 <= (int)(num_val * 1024.0))
  274. acodec->bit_rate = num_val * 1024.0;
  275. }
  276. if (!strcmp(key, "duration") ||
  277. !strcmp(key, "filesize") ||
  278. !strcmp(key, "width") ||
  279. !strcmp(key, "height") ||
  280. !strcmp(key, "videodatarate") ||
  281. !strcmp(key, "framerate") ||
  282. !strcmp(key, "videocodecid") ||
  283. !strcmp(key, "audiodatarate") ||
  284. !strcmp(key, "audiosamplerate") ||
  285. !strcmp(key, "audiosamplesize") ||
  286. !strcmp(key, "stereo") ||
  287. !strcmp(key, "audiocodecid"))
  288. return 0;
  289. if(amf_type == AMF_DATA_TYPE_BOOL) {
  290. av_strlcpy(str_val, num_val > 0 ? "true" : "false", sizeof(str_val));
  291. av_dict_set(&s->metadata, key, str_val, 0);
  292. } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
  293. snprintf(str_val, sizeof(str_val), "%.f", num_val);
  294. av_dict_set(&s->metadata, key, str_val, 0);
  295. } else if (amf_type == AMF_DATA_TYPE_STRING)
  296. av_dict_set(&s->metadata, key, str_val, 0);
  297. }
  298. return 0;
  299. }
  300. static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
  301. AMFDataType type;
  302. AVStream *stream, *astream, *vstream;
  303. AVIOContext *ioc;
  304. int i;
  305. char buffer[11]; //only needs to hold the string "onMetaData". Anything longer is something we don't want.
  306. astream = NULL;
  307. vstream = NULL;
  308. ioc = s->pb;
  309. //first object needs to be "onMetaData" string
  310. type = avio_r8(ioc);
  311. if(type != AMF_DATA_TYPE_STRING || amf_get_string(ioc, buffer, sizeof(buffer)) < 0 || strcmp(buffer, "onMetaData"))
  312. return -1;
  313. //find the streams now so that amf_parse_object doesn't need to do the lookup every time it is called.
  314. for(i = 0; i < s->nb_streams; i++) {
  315. stream = s->streams[i];
  316. if (stream->codec->codec_type == AVMEDIA_TYPE_AUDIO) astream = stream;
  317. else if(stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) vstream = stream;
  318. }
  319. //parse the second object (we want a mixed array)
  320. if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
  321. return -1;
  322. return 0;
  323. }
  324. static AVStream *create_stream(AVFormatContext *s, int is_audio){
  325. AVStream *st = avformat_new_stream(s, NULL);
  326. if (!st)
  327. return NULL;
  328. st->id = is_audio;
  329. st->codec->codec_type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
  330. avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
  331. return st;
  332. }
  333. static int flv_read_header(AVFormatContext *s)
  334. {
  335. int offset, flags;
  336. avio_skip(s->pb, 4);
  337. flags = avio_r8(s->pb);
  338. /* old flvtool cleared this field */
  339. /* FIXME: better fix needed */
  340. if (!flags) {
  341. flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
  342. av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
  343. }
  344. if((flags & (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
  345. != (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
  346. s->ctx_flags |= AVFMTCTX_NOHEADER;
  347. if(flags & FLV_HEADER_FLAG_HASVIDEO){
  348. if(!create_stream(s, 0))
  349. return AVERROR(ENOMEM);
  350. }
  351. if(flags & FLV_HEADER_FLAG_HASAUDIO){
  352. if(!create_stream(s, 1))
  353. return AVERROR(ENOMEM);
  354. }
  355. offset = avio_rb32(s->pb);
  356. avio_seek(s->pb, offset, SEEK_SET);
  357. avio_skip(s->pb, 4);
  358. s->start_time = 0;
  359. return 0;
  360. }
  361. static int flv_read_close(AVFormatContext *s)
  362. {
  363. FLVContext *flv = s->priv_data;
  364. av_freep(&flv->new_extradata[0]);
  365. av_freep(&flv->new_extradata[1]);
  366. return 0;
  367. }
  368. static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
  369. {
  370. av_free(st->codec->extradata);
  371. st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  372. if (!st->codec->extradata)
  373. return AVERROR(ENOMEM);
  374. st->codec->extradata_size = size;
  375. avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
  376. return 0;
  377. }
  378. static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
  379. int size)
  380. {
  381. av_free(flv->new_extradata[stream]);
  382. flv->new_extradata[stream] = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  383. if (!flv->new_extradata[stream])
  384. return AVERROR(ENOMEM);
  385. flv->new_extradata_size[stream] = size;
  386. avio_read(pb, flv->new_extradata[stream], size);
  387. return 0;
  388. }
  389. static void clear_index_entries(AVFormatContext *s, int64_t pos)
  390. {
  391. int i, j, out;
  392. av_log(s, AV_LOG_WARNING, "Found invalid index entries, clearing the index.\n");
  393. for (i = 0; i < s->nb_streams; i++) {
  394. AVStream *st = s->streams[i];
  395. /* Remove all index entries that point to >= pos */
  396. out = 0;
  397. for (j = 0; j < st->nb_index_entries; j++) {
  398. if (st->index_entries[j].pos < pos)
  399. st->index_entries[out++] = st->index_entries[j];
  400. }
  401. st->nb_index_entries = out;
  402. }
  403. }
  404. static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
  405. {
  406. FLVContext *flv = s->priv_data;
  407. int ret, i, type, size, flags, is_audio;
  408. int64_t next, pos;
  409. int64_t dts, pts = AV_NOPTS_VALUE;
  410. int sample_rate = 0, channels = 0;
  411. AVStream *st = NULL;
  412. for(;;avio_skip(s->pb, 4)){ /* pkt size is repeated at end. skip it */
  413. pos = avio_tell(s->pb);
  414. type = avio_r8(s->pb);
  415. size = avio_rb24(s->pb);
  416. dts = avio_rb24(s->pb);
  417. dts |= avio_r8(s->pb) << 24;
  418. av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
  419. if (s->pb->eof_reached)
  420. return AVERROR_EOF;
  421. avio_skip(s->pb, 3); /* stream id, always 0 */
  422. flags = 0;
  423. if (flv->validate_next < flv->validate_count) {
  424. int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
  425. if (pos == validate_pos) {
  426. if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
  427. VALIDATE_INDEX_TS_THRESH) {
  428. flv->validate_next++;
  429. } else {
  430. clear_index_entries(s, validate_pos);
  431. flv->validate_count = 0;
  432. }
  433. } else if (pos > validate_pos) {
  434. clear_index_entries(s, validate_pos);
  435. flv->validate_count = 0;
  436. }
  437. }
  438. if(size == 0)
  439. continue;
  440. next= size + avio_tell(s->pb);
  441. if (type == FLV_TAG_TYPE_AUDIO) {
  442. is_audio=1;
  443. flags = avio_r8(s->pb);
  444. size--;
  445. } else if (type == FLV_TAG_TYPE_VIDEO) {
  446. is_audio=0;
  447. flags = avio_r8(s->pb);
  448. size--;
  449. if ((flags & 0xf0) == 0x50) /* video info / command frame */
  450. goto skip;
  451. } else {
  452. if (type == FLV_TAG_TYPE_META && size > 13+1+4)
  453. flv_read_metabody(s, next);
  454. else /* skip packet */
  455. av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
  456. skip:
  457. avio_seek(s->pb, next, SEEK_SET);
  458. continue;
  459. }
  460. /* skip empty data packets */
  461. if (!size)
  462. continue;
  463. /* now find stream */
  464. for(i=0;i<s->nb_streams;i++) {
  465. st = s->streams[i];
  466. if (st->id == is_audio)
  467. break;
  468. }
  469. if(i == s->nb_streams){
  470. av_log(s, AV_LOG_ERROR, "invalid stream\n");
  471. st= create_stream(s, is_audio);
  472. s->ctx_flags &= ~AVFMTCTX_NOHEADER;
  473. }
  474. av_dlog(s, "%d %X %d \n", is_audio, flags, st->discard);
  475. if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio))
  476. ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
  477. || st->discard >= AVDISCARD_ALL
  478. ){
  479. avio_seek(s->pb, next, SEEK_SET);
  480. continue;
  481. }
  482. if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
  483. av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
  484. break;
  485. }
  486. // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps
  487. if(s->pb->seekable && (!s->duration || s->duration==AV_NOPTS_VALUE)){
  488. int size;
  489. const int64_t pos= avio_tell(s->pb);
  490. const int64_t fsize= avio_size(s->pb);
  491. avio_seek(s->pb, fsize-4, SEEK_SET);
  492. size= avio_rb32(s->pb);
  493. avio_seek(s->pb, fsize-3-size, SEEK_SET);
  494. if(size == avio_rb24(s->pb) + 11){
  495. uint32_t ts = avio_rb24(s->pb);
  496. ts |= avio_r8(s->pb) << 24;
  497. s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
  498. }
  499. avio_seek(s->pb, pos, SEEK_SET);
  500. }
  501. if(is_audio){
  502. int bits_per_coded_sample;
  503. channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
  504. sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
  505. bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
  506. if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
  507. st->codec->channels = channels;
  508. st->codec->sample_rate = sample_rate;
  509. st->codec->bits_per_coded_sample = bits_per_coded_sample;
  510. }
  511. if(!st->codec->codec_id){
  512. flv_set_audio_codec(s, st, st->codec, flags & FLV_AUDIO_CODECID_MASK);
  513. flv->last_sample_rate = sample_rate = st->codec->sample_rate;
  514. flv->last_channels = channels = st->codec->channels;
  515. } else {
  516. AVCodecContext ctx;
  517. ctx.sample_rate = sample_rate;
  518. flv_set_audio_codec(s, st, &ctx, flags & FLV_AUDIO_CODECID_MASK);
  519. sample_rate = ctx.sample_rate;
  520. }
  521. }else{
  522. size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
  523. }
  524. if (st->codec->codec_id == CODEC_ID_AAC ||
  525. st->codec->codec_id == CODEC_ID_H264) {
  526. int type = avio_r8(s->pb);
  527. size--;
  528. if (st->codec->codec_id == CODEC_ID_H264) {
  529. int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000; // sign extension
  530. pts = dts + cts;
  531. if (cts < 0) { // dts are wrong
  532. flv->wrong_dts = 1;
  533. av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
  534. }
  535. if (flv->wrong_dts)
  536. dts = AV_NOPTS_VALUE;
  537. }
  538. if (type == 0) {
  539. if (st->codec->extradata) {
  540. if ((ret = flv_queue_extradata(flv, s->pb, is_audio, size)) < 0)
  541. return ret;
  542. ret = AVERROR(EAGAIN);
  543. goto leave;
  544. }
  545. if ((ret = flv_get_extradata(s, st, size)) < 0)
  546. return ret;
  547. if (st->codec->codec_id == CODEC_ID_AAC) {
  548. MPEG4AudioConfig cfg;
  549. avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
  550. st->codec->extradata_size * 8, 1);
  551. st->codec->channels = cfg.channels;
  552. if (cfg.ext_sample_rate)
  553. st->codec->sample_rate = cfg.ext_sample_rate;
  554. else
  555. st->codec->sample_rate = cfg.sample_rate;
  556. av_dlog(s, "mp4a config channels %d sample rate %d\n",
  557. st->codec->channels, st->codec->sample_rate);
  558. }
  559. ret = AVERROR(EAGAIN);
  560. goto leave;
  561. }
  562. }
  563. /* skip empty data packets */
  564. if (!size) {
  565. ret = AVERROR(EAGAIN);
  566. goto leave;
  567. }
  568. ret= av_get_packet(s->pb, pkt, size);
  569. if (ret < 0) {
  570. return AVERROR(EIO);
  571. }
  572. /* note: we need to modify the packet size here to handle the last
  573. packet */
  574. pkt->size = ret;
  575. pkt->dts = dts;
  576. pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
  577. pkt->stream_index = st->index;
  578. if (flv->new_extradata[is_audio]) {
  579. uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
  580. flv->new_extradata_size[is_audio]);
  581. if (side) {
  582. memcpy(side, flv->new_extradata[is_audio],
  583. flv->new_extradata_size[is_audio]);
  584. av_freep(&flv->new_extradata[is_audio]);
  585. flv->new_extradata_size[is_audio] = 0;
  586. }
  587. }
  588. if (is_audio && (sample_rate != flv->last_sample_rate ||
  589. channels != flv->last_channels)) {
  590. flv->last_sample_rate = sample_rate;
  591. flv->last_channels = channels;
  592. ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
  593. }
  594. if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
  595. pkt->flags |= AV_PKT_FLAG_KEY;
  596. leave:
  597. avio_skip(s->pb, 4);
  598. return ret;
  599. }
  600. static int flv_read_seek(AVFormatContext *s, int stream_index,
  601. int64_t ts, int flags)
  602. {
  603. FLVContext *flv = s->priv_data;
  604. flv->validate_count = 0;
  605. return avio_seek_time(s->pb, stream_index, ts, flags);
  606. }
  607. #if 0 /* don't know enough to implement this */
  608. static int flv_read_seek2(AVFormatContext *s, int stream_index,
  609. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  610. {
  611. int ret = AVERROR(ENOSYS);
  612. if (ts - min_ts > (uint64_t)(max_ts - ts)) flags |= AVSEEK_FLAG_BACKWARD;
  613. if (!s->pb->seekable) {
  614. if (stream_index < 0) {
  615. stream_index = av_find_default_stream_index(s);
  616. if (stream_index < 0)
  617. return -1;
  618. /* timestamp for default must be expressed in AV_TIME_BASE units */
  619. ts = av_rescale_rnd(ts, 1000, AV_TIME_BASE,
  620. flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
  621. }
  622. ret = avio_seek_time(s->pb, stream_index, ts, flags);
  623. }
  624. if (ret == AVERROR(ENOSYS))
  625. ret = av_seek_frame(s, stream_index, ts, flags);
  626. return ret;
  627. }
  628. #endif
  629. AVInputFormat ff_flv_demuxer = {
  630. .name = "flv",
  631. .long_name = NULL_IF_CONFIG_SMALL("FLV format"),
  632. .priv_data_size = sizeof(FLVContext),
  633. .read_probe = flv_probe,
  634. .read_header = flv_read_header,
  635. .read_packet = flv_read_packet,
  636. .read_seek = flv_read_seek,
  637. #if 0
  638. .read_seek2 = flv_read_seek2,
  639. #endif
  640. .read_close = flv_read_close,
  641. .extensions = "flv",
  642. };