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.

1346 lines
47KB

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