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.

741 lines
27KB

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