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.

682 lines
24KB

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