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.

1300 lines
45KB

  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 4 bits: difference between encoded width and visible width
  8. * - lower 4 bits: 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/channel_layout.h"
  28. #include "libavutil/dict.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/intfloat.h"
  31. #include "libavutil/mathematics.h"
  32. #include "libavcodec/bytestream.h"
  33. #include "libavcodec/mpeg4audio.h"
  34. #include "avformat.h"
  35. #include "internal.h"
  36. #include "avio_internal.h"
  37. #include "flv.h"
  38. #define VALIDATE_INDEX_TS_THRESH 2500
  39. #define RESYNC_BUFFER_SIZE (1<<20)
  40. typedef struct FLVContext {
  41. const AVClass *class; ///< Class for private options.
  42. int trust_metadata; ///< configure streams according onMetaData
  43. int wrong_dts; ///< wrong dts due to negative cts
  44. uint8_t *new_extradata[FLV_STREAM_TYPE_NB];
  45. int new_extradata_size[FLV_STREAM_TYPE_NB];
  46. int last_sample_rate;
  47. int last_channels;
  48. struct {
  49. int64_t dts;
  50. int64_t pos;
  51. } validate_index[2];
  52. int validate_next;
  53. int validate_count;
  54. int searched_for_end;
  55. uint8_t resync_buffer[2*RESYNC_BUFFER_SIZE];
  56. int broken_sizes;
  57. int sum_flv_tag_size;
  58. int last_keyframe_stream_index;
  59. int keyframe_count;
  60. int64_t video_bit_rate;
  61. int64_t audio_bit_rate;
  62. int64_t *keyframe_times;
  63. int64_t *keyframe_filepositions;
  64. int missing_streams;
  65. AVRational framerate;
  66. } FLVContext;
  67. static int probe(AVProbeData *p, int live)
  68. {
  69. const uint8_t *d = p->buf;
  70. unsigned offset = AV_RB32(d + 5);
  71. if (d[0] == 'F' &&
  72. d[1] == 'L' &&
  73. d[2] == 'V' &&
  74. d[3] < 5 && d[5] == 0 &&
  75. offset + 100 < p->buf_size &&
  76. offset > 8) {
  77. int is_live = !memcmp(d + offset + 40, "NGINX RTMP", 10);
  78. if (live == is_live)
  79. return AVPROBE_SCORE_MAX;
  80. }
  81. return 0;
  82. }
  83. static int flv_probe(AVProbeData *p)
  84. {
  85. return probe(p, 0);
  86. }
  87. static int live_flv_probe(AVProbeData *p)
  88. {
  89. return probe(p, 1);
  90. }
  91. static void add_keyframes_index(AVFormatContext *s)
  92. {
  93. FLVContext *flv = s->priv_data;
  94. AVStream *stream = NULL;
  95. unsigned int i = 0;
  96. if (flv->last_keyframe_stream_index < 0) {
  97. av_log(s, AV_LOG_DEBUG, "keyframe stream hasn't been created\n");
  98. return;
  99. }
  100. av_assert0(flv->last_keyframe_stream_index <= s->nb_streams);
  101. stream = s->streams[flv->last_keyframe_stream_index];
  102. if (stream->nb_index_entries == 0) {
  103. for (i = 0; i < flv->keyframe_count; i++) {
  104. av_log(s, AV_LOG_TRACE, "keyframe filepositions = %"PRId64" times = %"PRId64"\n",
  105. flv->keyframe_filepositions[i], flv->keyframe_times[i] * 1000);
  106. av_add_index_entry(stream, flv->keyframe_filepositions[i],
  107. flv->keyframe_times[i] * 1000, 0, 0, AVINDEX_KEYFRAME);
  108. }
  109. } else
  110. av_log(s, AV_LOG_WARNING, "Skipping duplicate index\n");
  111. if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  112. av_freep(&flv->keyframe_times);
  113. av_freep(&flv->keyframe_filepositions);
  114. flv->keyframe_count = 0;
  115. }
  116. }
  117. static AVStream *create_stream(AVFormatContext *s, int codec_type)
  118. {
  119. FLVContext *flv = s->priv_data;
  120. AVStream *st = avformat_new_stream(s, NULL);
  121. if (!st)
  122. return NULL;
  123. st->codecpar->codec_type = codec_type;
  124. if (s->nb_streams>=3 ||( s->nb_streams==2
  125. && s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE
  126. && s->streams[1]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE))
  127. s->ctx_flags &= ~AVFMTCTX_NOHEADER;
  128. if (codec_type == AVMEDIA_TYPE_AUDIO) {
  129. st->codecpar->bit_rate = flv->audio_bit_rate;
  130. flv->missing_streams &= ~FLV_HEADER_FLAG_HASAUDIO;
  131. }
  132. if (codec_type == AVMEDIA_TYPE_VIDEO) {
  133. st->codecpar->bit_rate = flv->video_bit_rate;
  134. flv->missing_streams &= ~FLV_HEADER_FLAG_HASVIDEO;
  135. st->avg_frame_rate = flv->framerate;
  136. }
  137. avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
  138. flv->last_keyframe_stream_index = s->nb_streams - 1;
  139. add_keyframes_index(s);
  140. return st;
  141. }
  142. static int flv_same_audio_codec(AVCodecParameters *apar, int flags)
  143. {
  144. int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
  145. int flv_codecid = flags & FLV_AUDIO_CODECID_MASK;
  146. int codec_id;
  147. if (!apar->codec_id && !apar->codec_tag)
  148. return 1;
  149. if (apar->bits_per_coded_sample != bits_per_coded_sample)
  150. return 0;
  151. switch (flv_codecid) {
  152. // no distinction between S16 and S8 PCM codec flags
  153. case FLV_CODECID_PCM:
  154. codec_id = bits_per_coded_sample == 8
  155. ? AV_CODEC_ID_PCM_U8
  156. #if HAVE_BIGENDIAN
  157. : AV_CODEC_ID_PCM_S16BE;
  158. #else
  159. : AV_CODEC_ID_PCM_S16LE;
  160. #endif
  161. return codec_id == apar->codec_id;
  162. case FLV_CODECID_PCM_LE:
  163. codec_id = bits_per_coded_sample == 8
  164. ? AV_CODEC_ID_PCM_U8
  165. : AV_CODEC_ID_PCM_S16LE;
  166. return codec_id == apar->codec_id;
  167. case FLV_CODECID_AAC:
  168. return apar->codec_id == AV_CODEC_ID_AAC;
  169. case FLV_CODECID_ADPCM:
  170. return apar->codec_id == AV_CODEC_ID_ADPCM_SWF;
  171. case FLV_CODECID_SPEEX:
  172. return apar->codec_id == AV_CODEC_ID_SPEEX;
  173. case FLV_CODECID_MP3:
  174. return apar->codec_id == AV_CODEC_ID_MP3;
  175. case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
  176. case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
  177. case FLV_CODECID_NELLYMOSER:
  178. return apar->codec_id == AV_CODEC_ID_NELLYMOSER;
  179. case FLV_CODECID_PCM_MULAW:
  180. return apar->sample_rate == 8000 &&
  181. apar->codec_id == AV_CODEC_ID_PCM_MULAW;
  182. case FLV_CODECID_PCM_ALAW:
  183. return apar->sample_rate == 8000 &&
  184. apar->codec_id == AV_CODEC_ID_PCM_ALAW;
  185. default:
  186. return apar->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
  187. }
  188. }
  189. static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream,
  190. AVCodecParameters *apar, int flv_codecid)
  191. {
  192. switch (flv_codecid) {
  193. // no distinction between S16 and S8 PCM codec flags
  194. case FLV_CODECID_PCM:
  195. apar->codec_id = apar->bits_per_coded_sample == 8
  196. ? AV_CODEC_ID_PCM_U8
  197. #if HAVE_BIGENDIAN
  198. : AV_CODEC_ID_PCM_S16BE;
  199. #else
  200. : AV_CODEC_ID_PCM_S16LE;
  201. #endif
  202. break;
  203. case FLV_CODECID_PCM_LE:
  204. apar->codec_id = apar->bits_per_coded_sample == 8
  205. ? AV_CODEC_ID_PCM_U8
  206. : AV_CODEC_ID_PCM_S16LE;
  207. break;
  208. case FLV_CODECID_AAC:
  209. apar->codec_id = AV_CODEC_ID_AAC;
  210. break;
  211. case FLV_CODECID_ADPCM:
  212. apar->codec_id = AV_CODEC_ID_ADPCM_SWF;
  213. break;
  214. case FLV_CODECID_SPEEX:
  215. apar->codec_id = AV_CODEC_ID_SPEEX;
  216. apar->sample_rate = 16000;
  217. break;
  218. case FLV_CODECID_MP3:
  219. apar->codec_id = AV_CODEC_ID_MP3;
  220. astream->need_parsing = AVSTREAM_PARSE_FULL;
  221. break;
  222. case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
  223. // in case metadata does not otherwise declare samplerate
  224. apar->sample_rate = 8000;
  225. apar->codec_id = AV_CODEC_ID_NELLYMOSER;
  226. break;
  227. case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
  228. apar->sample_rate = 16000;
  229. apar->codec_id = AV_CODEC_ID_NELLYMOSER;
  230. break;
  231. case FLV_CODECID_NELLYMOSER:
  232. apar->codec_id = AV_CODEC_ID_NELLYMOSER;
  233. break;
  234. case FLV_CODECID_PCM_MULAW:
  235. apar->sample_rate = 8000;
  236. apar->codec_id = AV_CODEC_ID_PCM_MULAW;
  237. break;
  238. case FLV_CODECID_PCM_ALAW:
  239. apar->sample_rate = 8000;
  240. apar->codec_id = AV_CODEC_ID_PCM_ALAW;
  241. break;
  242. default:
  243. avpriv_request_sample(s, "Audio codec (%x)",
  244. flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
  245. apar->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
  246. }
  247. }
  248. static int flv_same_video_codec(AVCodecParameters *vpar, int flags)
  249. {
  250. int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;
  251. if (!vpar->codec_id && !vpar->codec_tag)
  252. return 1;
  253. switch (flv_codecid) {
  254. case FLV_CODECID_H263:
  255. return vpar->codec_id == AV_CODEC_ID_FLV1;
  256. case FLV_CODECID_SCREEN:
  257. return vpar->codec_id == AV_CODEC_ID_FLASHSV;
  258. case FLV_CODECID_SCREEN2:
  259. return vpar->codec_id == AV_CODEC_ID_FLASHSV2;
  260. case FLV_CODECID_VP6:
  261. return vpar->codec_id == AV_CODEC_ID_VP6F;
  262. case FLV_CODECID_VP6A:
  263. return vpar->codec_id == AV_CODEC_ID_VP6A;
  264. case FLV_CODECID_H264:
  265. return vpar->codec_id == AV_CODEC_ID_H264;
  266. default:
  267. return vpar->codec_tag == flv_codecid;
  268. }
  269. }
  270. static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream,
  271. int flv_codecid, int read)
  272. {
  273. int ret = 0;
  274. AVCodecParameters *par = vstream->codecpar;
  275. enum AVCodecID old_codec_id = vstream->codecpar->codec_id;
  276. switch (flv_codecid) {
  277. case FLV_CODECID_H263:
  278. par->codec_id = AV_CODEC_ID_FLV1;
  279. break;
  280. case FLV_CODECID_REALH263:
  281. par->codec_id = AV_CODEC_ID_H263;
  282. break; // Really mean it this time
  283. case FLV_CODECID_SCREEN:
  284. par->codec_id = AV_CODEC_ID_FLASHSV;
  285. break;
  286. case FLV_CODECID_SCREEN2:
  287. par->codec_id = AV_CODEC_ID_FLASHSV2;
  288. break;
  289. case FLV_CODECID_VP6:
  290. par->codec_id = AV_CODEC_ID_VP6F;
  291. case FLV_CODECID_VP6A:
  292. if (flv_codecid == FLV_CODECID_VP6A)
  293. par->codec_id = AV_CODEC_ID_VP6A;
  294. if (read) {
  295. if (par->extradata_size != 1) {
  296. ff_alloc_extradata(par, 1);
  297. }
  298. if (par->extradata)
  299. par->extradata[0] = avio_r8(s->pb);
  300. else
  301. avio_skip(s->pb, 1);
  302. }
  303. ret = 1; // 1 byte body size adjustment for flv_read_packet()
  304. break;
  305. case FLV_CODECID_H264:
  306. par->codec_id = AV_CODEC_ID_H264;
  307. vstream->need_parsing = AVSTREAM_PARSE_HEADERS;
  308. ret = 3; // not 4, reading packet type will consume one byte
  309. break;
  310. case FLV_CODECID_MPEG4:
  311. par->codec_id = AV_CODEC_ID_MPEG4;
  312. ret = 3;
  313. break;
  314. default:
  315. avpriv_request_sample(s, "Video codec (%x)", flv_codecid);
  316. par->codec_tag = flv_codecid;
  317. }
  318. if (!vstream->internal->need_context_update && par->codec_id != old_codec_id) {
  319. avpriv_request_sample(s, "Changing the codec id midstream");
  320. return AVERROR_PATCHWELCOME;
  321. }
  322. return ret;
  323. }
  324. static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
  325. {
  326. int length = avio_rb16(ioc);
  327. if (length >= buffsize) {
  328. avio_skip(ioc, length);
  329. return -1;
  330. }
  331. avio_read(ioc, buffer, length);
  332. buffer[length] = '\0';
  333. return length;
  334. }
  335. static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, int64_t max_pos)
  336. {
  337. FLVContext *flv = s->priv_data;
  338. unsigned int timeslen = 0, fileposlen = 0, i;
  339. char str_val[256];
  340. int64_t *times = NULL;
  341. int64_t *filepositions = NULL;
  342. int ret = AVERROR(ENOSYS);
  343. int64_t initial_pos = avio_tell(ioc);
  344. if (flv->keyframe_count > 0) {
  345. av_log(s, AV_LOG_DEBUG, "keyframes have been paresed\n");
  346. return 0;
  347. }
  348. av_assert0(!flv->keyframe_times);
  349. av_assert0(!flv->keyframe_filepositions);
  350. if (s->flags & AVFMT_FLAG_IGNIDX)
  351. return 0;
  352. while (avio_tell(ioc) < max_pos - 2 &&
  353. amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
  354. int64_t **current_array;
  355. unsigned int arraylen;
  356. // Expect array object in context
  357. if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
  358. break;
  359. arraylen = avio_rb32(ioc);
  360. if (arraylen>>28)
  361. break;
  362. if (!strcmp(KEYFRAMES_TIMESTAMP_TAG , str_val) && !times) {
  363. current_array = &times;
  364. timeslen = arraylen;
  365. } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) &&
  366. !filepositions) {
  367. current_array = &filepositions;
  368. fileposlen = arraylen;
  369. } else
  370. // unexpected metatag inside keyframes, will not use such
  371. // metadata for indexing
  372. break;
  373. if (!(*current_array = av_mallocz(sizeof(**current_array) * arraylen))) {
  374. ret = AVERROR(ENOMEM);
  375. goto finish;
  376. }
  377. for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
  378. if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
  379. goto invalid;
  380. current_array[0][i] = av_int2double(avio_rb64(ioc));
  381. }
  382. if (times && filepositions) {
  383. // All done, exiting at a position allowing amf_parse_object
  384. // to finish parsing the object
  385. ret = 0;
  386. break;
  387. }
  388. }
  389. if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
  390. for (i = 0; i < FFMIN(2,fileposlen); i++) {
  391. flv->validate_index[i].pos = filepositions[i];
  392. flv->validate_index[i].dts = times[i] * 1000;
  393. flv->validate_count = i + 1;
  394. }
  395. flv->keyframe_times = times;
  396. flv->keyframe_filepositions = filepositions;
  397. flv->keyframe_count = timeslen;
  398. times = NULL;
  399. filepositions = NULL;
  400. } else {
  401. invalid:
  402. av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
  403. }
  404. finish:
  405. av_freep(&times);
  406. av_freep(&filepositions);
  407. avio_seek(ioc, initial_pos, SEEK_SET);
  408. return ret;
  409. }
  410. static int amf_parse_object(AVFormatContext *s, AVStream *astream,
  411. AVStream *vstream, const char *key,
  412. int64_t max_pos, int depth)
  413. {
  414. AVCodecParameters *apar, *vpar;
  415. FLVContext *flv = s->priv_data;
  416. AVIOContext *ioc;
  417. AMFDataType amf_type;
  418. char str_val[1024];
  419. double num_val;
  420. num_val = 0;
  421. ioc = s->pb;
  422. amf_type = avio_r8(ioc);
  423. switch (amf_type) {
  424. case AMF_DATA_TYPE_NUMBER:
  425. num_val = av_int2double(avio_rb64(ioc));
  426. break;
  427. case AMF_DATA_TYPE_BOOL:
  428. num_val = avio_r8(ioc);
  429. break;
  430. case AMF_DATA_TYPE_STRING:
  431. if (amf_get_string(ioc, str_val, sizeof(str_val)) < 0) {
  432. av_log(s, AV_LOG_ERROR, "AMF_DATA_TYPE_STRING parsing failed\n");
  433. return -1;
  434. }
  435. break;
  436. case AMF_DATA_TYPE_OBJECT:
  437. if (key &&
  438. ioc->seekable &&
  439. !strcmp(KEYFRAMES_TAG, key) && depth == 1)
  440. if (parse_keyframes_index(s, ioc,
  441. max_pos) < 0)
  442. av_log(s, AV_LOG_ERROR, "Keyframe index parsing failed\n");
  443. else
  444. add_keyframes_index(s);
  445. while (avio_tell(ioc) < max_pos - 2 &&
  446. amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
  447. if (amf_parse_object(s, astream, vstream, str_val, max_pos,
  448. depth + 1) < 0)
  449. return -1; // if we couldn't skip, bomb out.
  450. if (avio_r8(ioc) != AMF_END_OF_OBJECT) {
  451. av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_OBJECT\n");
  452. return -1;
  453. }
  454. break;
  455. case AMF_DATA_TYPE_NULL:
  456. case AMF_DATA_TYPE_UNDEFINED:
  457. case AMF_DATA_TYPE_UNSUPPORTED:
  458. break; // these take up no additional space
  459. case AMF_DATA_TYPE_MIXEDARRAY:
  460. {
  461. unsigned v;
  462. avio_skip(ioc, 4); // skip 32-bit max array index
  463. while (avio_tell(ioc) < max_pos - 2 &&
  464. amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
  465. // this is the only case in which we would want a nested
  466. // parse to not skip over the object
  467. if (amf_parse_object(s, astream, vstream, str_val, max_pos,
  468. depth + 1) < 0)
  469. return -1;
  470. v = avio_r8(ioc);
  471. if (v != AMF_END_OF_OBJECT) {
  472. av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_MIXEDARRAY, found %d\n", v);
  473. return -1;
  474. }
  475. break;
  476. }
  477. case AMF_DATA_TYPE_ARRAY:
  478. {
  479. unsigned int arraylen, i;
  480. arraylen = avio_rb32(ioc);
  481. for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++)
  482. if (amf_parse_object(s, NULL, NULL, NULL, max_pos,
  483. depth + 1) < 0)
  484. return -1; // if we couldn't skip, bomb out.
  485. }
  486. break;
  487. case AMF_DATA_TYPE_DATE:
  488. avio_skip(ioc, 8 + 2); // timestamp (double) and UTC offset (int16)
  489. break;
  490. default: // unsupported type, we couldn't skip
  491. av_log(s, AV_LOG_ERROR, "unsupported amf type %d\n", amf_type);
  492. return -1;
  493. }
  494. if (key) {
  495. apar = astream ? astream->codecpar : NULL;
  496. vpar = vstream ? vstream->codecpar : NULL;
  497. // stream info doesn't live any deeper than the first object
  498. if (depth == 1) {
  499. if (amf_type == AMF_DATA_TYPE_NUMBER ||
  500. amf_type == AMF_DATA_TYPE_BOOL) {
  501. if (!strcmp(key, "duration"))
  502. s->duration = num_val * AV_TIME_BASE;
  503. else if (!strcmp(key, "videodatarate") &&
  504. 0 <= (int)(num_val * 1024.0))
  505. flv->video_bit_rate = num_val * 1024.0;
  506. else if (!strcmp(key, "audiodatarate") &&
  507. 0 <= (int)(num_val * 1024.0))
  508. flv->audio_bit_rate = num_val * 1024.0;
  509. else if (!strcmp(key, "datastream")) {
  510. AVStream *st = create_stream(s, AVMEDIA_TYPE_SUBTITLE);
  511. if (!st)
  512. return AVERROR(ENOMEM);
  513. st->codecpar->codec_id = AV_CODEC_ID_TEXT;
  514. } else if (!strcmp(key, "framerate")) {
  515. flv->framerate = av_d2q(num_val, 1000);
  516. if (vstream)
  517. vstream->avg_frame_rate = flv->framerate;
  518. } else if (flv->trust_metadata) {
  519. if (!strcmp(key, "videocodecid") && vpar) {
  520. int ret = flv_set_video_codec(s, vstream, num_val, 0);
  521. if (ret < 0)
  522. return ret;
  523. } else if (!strcmp(key, "audiocodecid") && apar) {
  524. int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
  525. flv_set_audio_codec(s, astream, apar, id);
  526. } else if (!strcmp(key, "audiosamplerate") && apar) {
  527. apar->sample_rate = num_val;
  528. } else if (!strcmp(key, "audiosamplesize") && apar) {
  529. apar->bits_per_coded_sample = num_val;
  530. } else if (!strcmp(key, "stereo") && apar) {
  531. apar->channels = num_val + 1;
  532. apar->channel_layout = apar->channels == 2 ?
  533. AV_CH_LAYOUT_STEREO :
  534. AV_CH_LAYOUT_MONO;
  535. } else if (!strcmp(key, "width") && vpar) {
  536. vpar->width = num_val;
  537. } else if (!strcmp(key, "height") && vpar) {
  538. vpar->height = num_val;
  539. }
  540. }
  541. }
  542. if (amf_type == AMF_DATA_TYPE_STRING) {
  543. if (!strcmp(key, "encoder")) {
  544. int version = -1;
  545. if (1 == sscanf(str_val, "Open Broadcaster Software v0.%d", &version)) {
  546. if (version > 0 && version <= 655)
  547. flv->broken_sizes = 1;
  548. }
  549. } else if (!strcmp(key, "metadatacreator") && !strcmp(str_val, "MEGA")) {
  550. flv->broken_sizes = 1;
  551. }
  552. }
  553. }
  554. if (amf_type == AMF_DATA_TYPE_OBJECT && s->nb_streams == 1 &&
  555. ((!apar && !strcmp(key, "audiocodecid")) ||
  556. (!vpar && !strcmp(key, "videocodecid"))))
  557. s->ctx_flags &= ~AVFMTCTX_NOHEADER; //If there is either audio/video missing, codecid will be an empty object
  558. if (!strcmp(key, "duration") ||
  559. !strcmp(key, "filesize") ||
  560. !strcmp(key, "width") ||
  561. !strcmp(key, "height") ||
  562. !strcmp(key, "videodatarate") ||
  563. !strcmp(key, "framerate") ||
  564. !strcmp(key, "videocodecid") ||
  565. !strcmp(key, "audiodatarate") ||
  566. !strcmp(key, "audiosamplerate") ||
  567. !strcmp(key, "audiosamplesize") ||
  568. !strcmp(key, "stereo") ||
  569. !strcmp(key, "audiocodecid") ||
  570. !strcmp(key, "datastream"))
  571. return 0;
  572. s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  573. if (amf_type == AMF_DATA_TYPE_BOOL) {
  574. av_strlcpy(str_val, num_val > 0 ? "true" : "false",
  575. sizeof(str_val));
  576. av_dict_set(&s->metadata, key, str_val, 0);
  577. } else if (amf_type == AMF_DATA_TYPE_NUMBER) {
  578. snprintf(str_val, sizeof(str_val), "%.f", num_val);
  579. av_dict_set(&s->metadata, key, str_val, 0);
  580. } else if (amf_type == AMF_DATA_TYPE_STRING)
  581. av_dict_set(&s->metadata, key, str_val, 0);
  582. }
  583. return 0;
  584. }
  585. #define TYPE_ONTEXTDATA 1
  586. #define TYPE_ONCAPTION 2
  587. #define TYPE_ONCAPTIONINFO 3
  588. #define TYPE_UNKNOWN 9
  589. static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
  590. {
  591. FLVContext *flv = s->priv_data;
  592. AMFDataType type;
  593. AVStream *stream, *astream, *vstream;
  594. AVStream av_unused *dstream;
  595. AVIOContext *ioc;
  596. int i;
  597. // only needs to hold the string "onMetaData".
  598. // Anything longer is something we don't want.
  599. char buffer[32];
  600. astream = NULL;
  601. vstream = NULL;
  602. dstream = NULL;
  603. ioc = s->pb;
  604. // first object needs to be "onMetaData" string
  605. type = avio_r8(ioc);
  606. if (type != AMF_DATA_TYPE_STRING ||
  607. amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
  608. return TYPE_UNKNOWN;
  609. if (!strcmp(buffer, "onTextData"))
  610. return TYPE_ONTEXTDATA;
  611. if (!strcmp(buffer, "onCaption"))
  612. return TYPE_ONCAPTION;
  613. if (!strcmp(buffer, "onCaptionInfo"))
  614. return TYPE_ONCAPTIONINFO;
  615. if (strcmp(buffer, "onMetaData") && strcmp(buffer, "onCuePoint")) {
  616. av_log(s, AV_LOG_DEBUG, "Unknown type %s\n", buffer);
  617. return TYPE_UNKNOWN;
  618. }
  619. // find the streams now so that amf_parse_object doesn't need to do
  620. // the lookup every time it is called.
  621. for (i = 0; i < s->nb_streams; i++) {
  622. stream = s->streams[i];
  623. if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  624. vstream = stream;
  625. flv->last_keyframe_stream_index = i;
  626. } else if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  627. astream = stream;
  628. if (flv->last_keyframe_stream_index == -1)
  629. flv->last_keyframe_stream_index = i;
  630. }
  631. else if (stream->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
  632. dstream = stream;
  633. }
  634. // parse the second object (we want a mixed array)
  635. if (amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
  636. return -1;
  637. return 0;
  638. }
  639. static int flv_read_header(AVFormatContext *s)
  640. {
  641. int flags;
  642. FLVContext *flv = s->priv_data;
  643. int offset;
  644. avio_skip(s->pb, 4);
  645. flags = avio_r8(s->pb);
  646. flv->missing_streams = flags & (FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO);
  647. s->ctx_flags |= AVFMTCTX_NOHEADER;
  648. offset = avio_rb32(s->pb);
  649. avio_seek(s->pb, offset, SEEK_SET);
  650. avio_skip(s->pb, 4);
  651. s->start_time = 0;
  652. flv->sum_flv_tag_size = 0;
  653. flv->last_keyframe_stream_index = -1;
  654. return 0;
  655. }
  656. static int flv_read_close(AVFormatContext *s)
  657. {
  658. int i;
  659. FLVContext *flv = s->priv_data;
  660. for (i=0; i<FLV_STREAM_TYPE_NB; i++)
  661. av_freep(&flv->new_extradata[i]);
  662. av_freep(&flv->keyframe_times);
  663. av_freep(&flv->keyframe_filepositions);
  664. return 0;
  665. }
  666. static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
  667. {
  668. av_freep(&st->codecpar->extradata);
  669. if (ff_get_extradata(s, st->codecpar, s->pb, size) < 0)
  670. return AVERROR(ENOMEM);
  671. return 0;
  672. }
  673. static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
  674. int size)
  675. {
  676. av_free(flv->new_extradata[stream]);
  677. flv->new_extradata[stream] = av_mallocz(size +
  678. AV_INPUT_BUFFER_PADDING_SIZE);
  679. if (!flv->new_extradata[stream])
  680. return AVERROR(ENOMEM);
  681. flv->new_extradata_size[stream] = size;
  682. avio_read(pb, flv->new_extradata[stream], size);
  683. return 0;
  684. }
  685. static void clear_index_entries(AVFormatContext *s, int64_t pos)
  686. {
  687. int i, j, out;
  688. av_log(s, AV_LOG_WARNING,
  689. "Found invalid index entries, clearing the index.\n");
  690. for (i = 0; i < s->nb_streams; i++) {
  691. AVStream *st = s->streams[i];
  692. /* Remove all index entries that point to >= pos */
  693. out = 0;
  694. for (j = 0; j < st->nb_index_entries; j++)
  695. if (st->index_entries[j].pos < pos)
  696. st->index_entries[out++] = st->index_entries[j];
  697. st->nb_index_entries = out;
  698. }
  699. }
  700. static int amf_skip_tag(AVIOContext *pb, AMFDataType type)
  701. {
  702. int nb = -1, ret, parse_name = 1;
  703. switch (type) {
  704. case AMF_DATA_TYPE_NUMBER:
  705. avio_skip(pb, 8);
  706. break;
  707. case AMF_DATA_TYPE_BOOL:
  708. avio_skip(pb, 1);
  709. break;
  710. case AMF_DATA_TYPE_STRING:
  711. avio_skip(pb, avio_rb16(pb));
  712. break;
  713. case AMF_DATA_TYPE_ARRAY:
  714. parse_name = 0;
  715. case AMF_DATA_TYPE_MIXEDARRAY:
  716. nb = avio_rb32(pb);
  717. case AMF_DATA_TYPE_OBJECT:
  718. while(!pb->eof_reached && (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY)) {
  719. if (parse_name) {
  720. int size = avio_rb16(pb);
  721. if (!size) {
  722. avio_skip(pb, 1);
  723. break;
  724. }
  725. avio_skip(pb, size);
  726. }
  727. if ((ret = amf_skip_tag(pb, avio_r8(pb))) < 0)
  728. return ret;
  729. }
  730. break;
  731. case AMF_DATA_TYPE_NULL:
  732. case AMF_DATA_TYPE_OBJECT_END:
  733. break;
  734. default:
  735. return AVERROR_INVALIDDATA;
  736. }
  737. return 0;
  738. }
  739. static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
  740. int64_t dts, int64_t next)
  741. {
  742. AVIOContext *pb = s->pb;
  743. AVStream *st = NULL;
  744. char buf[20];
  745. int ret = AVERROR_INVALIDDATA;
  746. int i, length = -1;
  747. int array = 0;
  748. switch (avio_r8(pb)) {
  749. case AMF_DATA_TYPE_ARRAY:
  750. array = 1;
  751. case AMF_DATA_TYPE_MIXEDARRAY:
  752. avio_seek(pb, 4, SEEK_CUR);
  753. case AMF_DATA_TYPE_OBJECT:
  754. break;
  755. default:
  756. goto skip;
  757. }
  758. while (array || (ret = amf_get_string(pb, buf, sizeof(buf))) > 0) {
  759. AMFDataType type = avio_r8(pb);
  760. if (type == AMF_DATA_TYPE_STRING && (array || !strcmp(buf, "text"))) {
  761. length = avio_rb16(pb);
  762. ret = av_get_packet(pb, pkt, length);
  763. if (ret < 0)
  764. goto skip;
  765. else
  766. break;
  767. } else {
  768. if ((ret = amf_skip_tag(pb, type)) < 0)
  769. goto skip;
  770. }
  771. }
  772. if (length < 0) {
  773. ret = AVERROR_INVALIDDATA;
  774. goto skip;
  775. }
  776. for (i = 0; i < s->nb_streams; i++) {
  777. st = s->streams[i];
  778. if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
  779. break;
  780. }
  781. if (i == s->nb_streams) {
  782. st = create_stream(s, AVMEDIA_TYPE_SUBTITLE);
  783. if (!st)
  784. return AVERROR(ENOMEM);
  785. st->codecpar->codec_id = AV_CODEC_ID_TEXT;
  786. }
  787. pkt->dts = dts;
  788. pkt->pts = dts;
  789. pkt->size = ret;
  790. pkt->stream_index = st->index;
  791. pkt->flags |= AV_PKT_FLAG_KEY;
  792. skip:
  793. avio_seek(s->pb, next + 4, SEEK_SET);
  794. return ret;
  795. }
  796. static int resync(AVFormatContext *s)
  797. {
  798. FLVContext *flv = s->priv_data;
  799. int64_t i;
  800. int64_t pos = avio_tell(s->pb);
  801. for (i=0; !avio_feof(s->pb); i++) {
  802. int j = i & (RESYNC_BUFFER_SIZE-1);
  803. int j1 = j + RESYNC_BUFFER_SIZE;
  804. flv->resync_buffer[j ] =
  805. flv->resync_buffer[j1] = avio_r8(s->pb);
  806. if (i > 22) {
  807. unsigned lsize2 = AV_RB32(flv->resync_buffer + j1 - 4);
  808. if (lsize2 >= 11 && lsize2 + 8LL < FFMIN(i, RESYNC_BUFFER_SIZE)) {
  809. unsigned size2 = AV_RB24(flv->resync_buffer + j1 - lsize2 + 1 - 4);
  810. unsigned lsize1 = AV_RB32(flv->resync_buffer + j1 - lsize2 - 8);
  811. if (lsize1 >= 11 && lsize1 + 8LL + lsize2 < FFMIN(i, RESYNC_BUFFER_SIZE)) {
  812. unsigned size1 = AV_RB24(flv->resync_buffer + j1 - lsize1 + 1 - lsize2 - 8);
  813. if (size1 == lsize1 - 11 && size2 == lsize2 - 11) {
  814. avio_seek(s->pb, pos + i - lsize1 - lsize2 - 8, SEEK_SET);
  815. return 1;
  816. }
  817. }
  818. }
  819. }
  820. }
  821. return AVERROR_EOF;
  822. }
  823. static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
  824. {
  825. FLVContext *flv = s->priv_data;
  826. int ret, i, size, flags;
  827. enum FlvTagType type;
  828. int stream_type=-1;
  829. int64_t next, pos, meta_pos;
  830. int64_t dts, pts = AV_NOPTS_VALUE;
  831. int av_uninit(channels);
  832. int av_uninit(sample_rate);
  833. AVStream *st = NULL;
  834. int last = -1;
  835. int orig_size;
  836. retry:
  837. /* pkt size is repeated at end. skip it */
  838. pos = avio_tell(s->pb);
  839. type = (avio_r8(s->pb) & 0x1F);
  840. orig_size =
  841. size = avio_rb24(s->pb);
  842. flv->sum_flv_tag_size += size + 11;
  843. dts = avio_rb24(s->pb);
  844. dts |= (unsigned)avio_r8(s->pb) << 24;
  845. av_log(s, AV_LOG_TRACE, "type:%d, size:%d, last:%d, dts:%"PRId64" pos:%"PRId64"\n", type, size, last, dts, avio_tell(s->pb));
  846. if (avio_feof(s->pb))
  847. return AVERROR_EOF;
  848. avio_skip(s->pb, 3); /* stream id, always 0 */
  849. flags = 0;
  850. if (flv->validate_next < flv->validate_count) {
  851. int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
  852. if (pos == validate_pos) {
  853. if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
  854. VALIDATE_INDEX_TS_THRESH) {
  855. flv->validate_next++;
  856. } else {
  857. clear_index_entries(s, validate_pos);
  858. flv->validate_count = 0;
  859. }
  860. } else if (pos > validate_pos) {
  861. clear_index_entries(s, validate_pos);
  862. flv->validate_count = 0;
  863. }
  864. }
  865. if (size == 0) {
  866. ret = FFERROR_REDO;
  867. goto leave;
  868. }
  869. next = size + avio_tell(s->pb);
  870. if (type == FLV_TAG_TYPE_AUDIO) {
  871. stream_type = FLV_STREAM_TYPE_AUDIO;
  872. flags = avio_r8(s->pb);
  873. size--;
  874. } else if (type == FLV_TAG_TYPE_VIDEO) {
  875. stream_type = FLV_STREAM_TYPE_VIDEO;
  876. flags = avio_r8(s->pb);
  877. size--;
  878. if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_VIDEO_INFO_CMD)
  879. goto skip;
  880. } else if (type == FLV_TAG_TYPE_META) {
  881. stream_type=FLV_STREAM_TYPE_DATA;
  882. if (size > 13 + 1 + 4) { // Header-type metadata stuff
  883. int type;
  884. meta_pos = avio_tell(s->pb);
  885. type = flv_read_metabody(s, next);
  886. if (type == 0 && dts == 0 || type < 0 || type == TYPE_UNKNOWN) {
  887. if (type < 0 && flv->validate_count &&
  888. flv->validate_index[0].pos > next &&
  889. flv->validate_index[0].pos - 4 < next
  890. ) {
  891. av_log(s, AV_LOG_WARNING, "Adjusting next position due to index mismatch\n");
  892. next = flv->validate_index[0].pos - 4;
  893. }
  894. goto skip;
  895. } else if (type == TYPE_ONTEXTDATA) {
  896. avpriv_request_sample(s, "OnTextData packet");
  897. return flv_data_packet(s, pkt, dts, next);
  898. } else if (type == TYPE_ONCAPTION) {
  899. return flv_data_packet(s, pkt, dts, next);
  900. }
  901. avio_seek(s->pb, meta_pos, SEEK_SET);
  902. }
  903. } else {
  904. av_log(s, AV_LOG_DEBUG,
  905. "Skipping flv packet: type %d, size %d, flags %d.\n",
  906. type, size, flags);
  907. skip:
  908. avio_seek(s->pb, next, SEEK_SET);
  909. ret = FFERROR_REDO;
  910. goto leave;
  911. }
  912. /* skip empty data packets */
  913. if (!size) {
  914. ret = FFERROR_REDO;
  915. goto leave;
  916. }
  917. /* now find stream */
  918. for (i = 0; i < s->nb_streams; i++) {
  919. st = s->streams[i];
  920. if (stream_type == FLV_STREAM_TYPE_AUDIO) {
  921. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  922. (s->audio_codec_id || flv_same_audio_codec(st->codecpar, flags)))
  923. break;
  924. } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
  925. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  926. (s->video_codec_id || flv_same_video_codec(st->codecpar, flags)))
  927. break;
  928. } else if (stream_type == FLV_STREAM_TYPE_DATA) {
  929. if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
  930. break;
  931. }
  932. }
  933. if (i == s->nb_streams) {
  934. static const enum AVMediaType stream_types[] = {AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_SUBTITLE};
  935. st = create_stream(s, stream_types[stream_type]);
  936. if (!st)
  937. return AVERROR(ENOMEM);
  938. }
  939. av_log(s, AV_LOG_TRACE, "%d %X %d \n", stream_type, flags, st->discard);
  940. if (s->pb->seekable &&
  941. ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||
  942. stream_type == FLV_STREAM_TYPE_AUDIO))
  943. av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
  944. if ( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || (stream_type == FLV_STREAM_TYPE_AUDIO)))
  945. ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && (stream_type == FLV_STREAM_TYPE_VIDEO)))
  946. || st->discard >= AVDISCARD_ALL
  947. ) {
  948. avio_seek(s->pb, next, SEEK_SET);
  949. ret = FFERROR_REDO;
  950. goto leave;
  951. }
  952. // if not streamed and no duration from metadata then seek to end to find
  953. // the duration from the timestamps
  954. if (s->pb->seekable && (!s->duration || s->duration == AV_NOPTS_VALUE) &&
  955. !flv->searched_for_end) {
  956. int size;
  957. const int64_t pos = avio_tell(s->pb);
  958. // Read the last 4 bytes of the file, this should be the size of the
  959. // previous FLV tag. Use the timestamp of its payload as duration.
  960. int64_t fsize = avio_size(s->pb);
  961. retry_duration:
  962. avio_seek(s->pb, fsize - 4, SEEK_SET);
  963. size = avio_rb32(s->pb);
  964. if (size > 0 && size < fsize) {
  965. // Seek to the start of the last FLV tag at position (fsize - 4 - size)
  966. // but skip the byte indicating the type.
  967. avio_seek(s->pb, fsize - 3 - size, SEEK_SET);
  968. if (size == avio_rb24(s->pb) + 11) {
  969. uint32_t ts = avio_rb24(s->pb);
  970. ts |= avio_r8(s->pb) << 24;
  971. if (ts)
  972. s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
  973. else if (fsize >= 8 && fsize - 8 >= size) {
  974. fsize -= size+4;
  975. goto retry_duration;
  976. }
  977. }
  978. }
  979. avio_seek(s->pb, pos, SEEK_SET);
  980. flv->searched_for_end = 1;
  981. }
  982. if (stream_type == FLV_STREAM_TYPE_AUDIO) {
  983. int bits_per_coded_sample;
  984. channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
  985. sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>
  986. FLV_AUDIO_SAMPLERATE_OFFSET) >> 3;
  987. bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
  988. if (!st->codecpar->channels || !st->codecpar->sample_rate ||
  989. !st->codecpar->bits_per_coded_sample) {
  990. st->codecpar->channels = channels;
  991. st->codecpar->channel_layout = channels == 1
  992. ? AV_CH_LAYOUT_MONO
  993. : AV_CH_LAYOUT_STEREO;
  994. st->codecpar->sample_rate = sample_rate;
  995. st->codecpar->bits_per_coded_sample = bits_per_coded_sample;
  996. }
  997. if (!st->codecpar->codec_id) {
  998. flv_set_audio_codec(s, st, st->codecpar,
  999. flags & FLV_AUDIO_CODECID_MASK);
  1000. flv->last_sample_rate =
  1001. sample_rate = st->codecpar->sample_rate;
  1002. flv->last_channels =
  1003. channels = st->codecpar->channels;
  1004. } else {
  1005. AVCodecParameters *par = avcodec_parameters_alloc();
  1006. if (!par) {
  1007. ret = AVERROR(ENOMEM);
  1008. goto leave;
  1009. }
  1010. par->sample_rate = sample_rate;
  1011. par->bits_per_coded_sample = bits_per_coded_sample;
  1012. flv_set_audio_codec(s, st, par, flags & FLV_AUDIO_CODECID_MASK);
  1013. sample_rate = par->sample_rate;
  1014. avcodec_parameters_free(&par);
  1015. }
  1016. } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
  1017. int ret = flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
  1018. if (ret < 0)
  1019. return ret;
  1020. size -= ret;
  1021. } else if (stream_type == FLV_STREAM_TYPE_DATA) {
  1022. st->codecpar->codec_id = AV_CODEC_ID_TEXT;
  1023. }
  1024. if (st->codecpar->codec_id == AV_CODEC_ID_AAC ||
  1025. st->codecpar->codec_id == AV_CODEC_ID_H264 ||
  1026. st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
  1027. int type = avio_r8(s->pb);
  1028. size--;
  1029. if (st->codecpar->codec_id == AV_CODEC_ID_H264 || st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
  1030. // sign extension
  1031. int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
  1032. pts = dts + cts;
  1033. if (cts < 0) { // dts might be wrong
  1034. if (!flv->wrong_dts)
  1035. av_log(s, AV_LOG_WARNING,
  1036. "Negative cts, previous timestamps might be wrong.\n");
  1037. flv->wrong_dts = 1;
  1038. } else if (FFABS(dts - pts) > 1000*60*15) {
  1039. av_log(s, AV_LOG_WARNING,
  1040. "invalid timestamps %"PRId64" %"PRId64"\n", dts, pts);
  1041. dts = pts = AV_NOPTS_VALUE;
  1042. }
  1043. }
  1044. if (type == 0 && (!st->codecpar->extradata || st->codecpar->codec_id == AV_CODEC_ID_AAC ||
  1045. st->codecpar->codec_id == AV_CODEC_ID_H264)) {
  1046. AVDictionaryEntry *t;
  1047. if (st->codecpar->extradata) {
  1048. if ((ret = flv_queue_extradata(flv, s->pb, stream_type, size)) < 0)
  1049. return ret;
  1050. ret = FFERROR_REDO;
  1051. goto leave;
  1052. }
  1053. if ((ret = flv_get_extradata(s, st, size)) < 0)
  1054. return ret;
  1055. /* Workaround for buggy Omnia A/XE encoder */
  1056. t = av_dict_get(s->metadata, "Encoder", NULL, 0);
  1057. if (st->codecpar->codec_id == AV_CODEC_ID_AAC && t && !strcmp(t->value, "Omnia A/XE"))
  1058. st->codecpar->extradata_size = 2;
  1059. if (st->codecpar->codec_id == AV_CODEC_ID_AAC && 0) {
  1060. MPEG4AudioConfig cfg;
  1061. if (avpriv_mpeg4audio_get_config(&cfg, st->codecpar->extradata,
  1062. st->codecpar->extradata_size * 8, 1) >= 0) {
  1063. st->codecpar->channels = cfg.channels;
  1064. st->codecpar->channel_layout = 0;
  1065. if (cfg.ext_sample_rate)
  1066. st->codecpar->sample_rate = cfg.ext_sample_rate;
  1067. else
  1068. st->codecpar->sample_rate = cfg.sample_rate;
  1069. av_log(s, AV_LOG_TRACE, "mp4a config channels %d sample rate %d\n",
  1070. st->codecpar->channels, st->codecpar->sample_rate);
  1071. }
  1072. }
  1073. ret = FFERROR_REDO;
  1074. goto leave;
  1075. }
  1076. }
  1077. /* skip empty data packets */
  1078. if (!size) {
  1079. ret = FFERROR_REDO;
  1080. goto leave;
  1081. }
  1082. ret = av_get_packet(s->pb, pkt, size);
  1083. if (ret < 0)
  1084. return ret;
  1085. pkt->dts = dts;
  1086. pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
  1087. pkt->stream_index = st->index;
  1088. pkt->pos = pos;
  1089. if (flv->new_extradata[stream_type]) {
  1090. uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
  1091. flv->new_extradata_size[stream_type]);
  1092. if (side) {
  1093. memcpy(side, flv->new_extradata[stream_type],
  1094. flv->new_extradata_size[stream_type]);
  1095. av_freep(&flv->new_extradata[stream_type]);
  1096. flv->new_extradata_size[stream_type] = 0;
  1097. }
  1098. }
  1099. if (stream_type == FLV_STREAM_TYPE_AUDIO &&
  1100. (sample_rate != flv->last_sample_rate ||
  1101. channels != flv->last_channels)) {
  1102. flv->last_sample_rate = sample_rate;
  1103. flv->last_channels = channels;
  1104. ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
  1105. }
  1106. if ( stream_type == FLV_STREAM_TYPE_AUDIO ||
  1107. ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY) ||
  1108. stream_type == FLV_STREAM_TYPE_DATA)
  1109. pkt->flags |= AV_PKT_FLAG_KEY;
  1110. leave:
  1111. last = avio_rb32(s->pb);
  1112. if (last != orig_size + 11 && last != orig_size + 10 &&
  1113. !avio_feof(s->pb) &&
  1114. (last != orig_size || !last) && last != flv->sum_flv_tag_size &&
  1115. !flv->broken_sizes) {
  1116. av_log(s, AV_LOG_ERROR, "Packet mismatch %d %d %d\n", last, orig_size + 11, flv->sum_flv_tag_size);
  1117. avio_seek(s->pb, pos + 1, SEEK_SET);
  1118. ret = resync(s);
  1119. av_packet_unref(pkt);
  1120. if (ret >= 0) {
  1121. goto retry;
  1122. }
  1123. }
  1124. return ret;
  1125. }
  1126. static int flv_read_seek(AVFormatContext *s, int stream_index,
  1127. int64_t ts, int flags)
  1128. {
  1129. FLVContext *flv = s->priv_data;
  1130. flv->validate_count = 0;
  1131. return avio_seek_time(s->pb, stream_index, ts, flags);
  1132. }
  1133. #define OFFSET(x) offsetof(FLVContext, x)
  1134. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1135. static const AVOption options[] = {
  1136. { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
  1137. { "missing_streams", "", OFFSET(missing_streams), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 0xFF, VD | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
  1138. { NULL }
  1139. };
  1140. static const AVClass flv_class = {
  1141. .class_name = "flvdec",
  1142. .item_name = av_default_item_name,
  1143. .option = options,
  1144. .version = LIBAVUTIL_VERSION_INT,
  1145. };
  1146. AVInputFormat ff_flv_demuxer = {
  1147. .name = "flv",
  1148. .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
  1149. .priv_data_size = sizeof(FLVContext),
  1150. .read_probe = flv_probe,
  1151. .read_header = flv_read_header,
  1152. .read_packet = flv_read_packet,
  1153. .read_seek = flv_read_seek,
  1154. .read_close = flv_read_close,
  1155. .extensions = "flv",
  1156. .priv_class = &flv_class,
  1157. };
  1158. static const AVClass live_flv_class = {
  1159. .class_name = "live_flvdec",
  1160. .item_name = av_default_item_name,
  1161. .option = options,
  1162. .version = LIBAVUTIL_VERSION_INT,
  1163. };
  1164. AVInputFormat ff_live_flv_demuxer = {
  1165. .name = "live_flv",
  1166. .long_name = NULL_IF_CONFIG_SMALL("live RTMP FLV (Flash Video)"),
  1167. .priv_data_size = sizeof(FLVContext),
  1168. .read_probe = live_flv_probe,
  1169. .read_header = flv_read_header,
  1170. .read_packet = flv_read_packet,
  1171. .read_seek = flv_read_seek,
  1172. .read_close = flv_read_close,
  1173. .extensions = "flv",
  1174. .priv_class = &live_flv_class,
  1175. .flags = AVFMT_TS_DISCONT
  1176. };