flvdec.c 26 KB

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