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.

456 lines
16KB

  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 "libavcodec/bytestream.h"
  28. #include "libavcodec/mpeg4audio.h"
  29. #include "avformat.h"
  30. #include "flv.h"
  31. typedef struct {
  32. int wrong_dts; ///< wrong dts due to negative cts
  33. } FLVContext;
  34. static int flv_probe(AVProbeData *p)
  35. {
  36. const uint8_t *d;
  37. d = p->buf;
  38. if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
  39. return AVPROBE_SCORE_MAX;
  40. }
  41. return 0;
  42. }
  43. static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, int flv_codecid) {
  44. AVCodecContext *acodec = astream->codec;
  45. switch(flv_codecid) {
  46. //no distinction between S16 and S8 PCM codec flags
  47. case FLV_CODECID_PCM:
  48. acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_S8 :
  49. #if HAVE_BIGENDIAN
  50. CODEC_ID_PCM_S16BE;
  51. #else
  52. CODEC_ID_PCM_S16LE;
  53. #endif
  54. break;
  55. case FLV_CODECID_PCM_LE:
  56. acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_S8 : CODEC_ID_PCM_S16LE; break;
  57. case FLV_CODECID_AAC : acodec->codec_id = CODEC_ID_AAC; break;
  58. case FLV_CODECID_ADPCM: acodec->codec_id = CODEC_ID_ADPCM_SWF; break;
  59. case FLV_CODECID_SPEEX:
  60. acodec->codec_id = CODEC_ID_SPEEX;
  61. acodec->sample_rate = 16000;
  62. break;
  63. case FLV_CODECID_MP3 : acodec->codec_id = CODEC_ID_MP3 ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
  64. case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
  65. acodec->sample_rate = 8000; //in case metadata does not otherwise declare samplerate
  66. case FLV_CODECID_NELLYMOSER:
  67. acodec->codec_id = CODEC_ID_NELLYMOSER;
  68. break;
  69. default:
  70. av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
  71. acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
  72. }
  73. }
  74. static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
  75. AVCodecContext *vcodec = vstream->codec;
  76. switch(flv_codecid) {
  77. case FLV_CODECID_H263 : vcodec->codec_id = CODEC_ID_FLV1 ; break;
  78. case FLV_CODECID_SCREEN: vcodec->codec_id = CODEC_ID_FLASHSV; break;
  79. case FLV_CODECID_VP6 : vcodec->codec_id = CODEC_ID_VP6F ;
  80. case FLV_CODECID_VP6A :
  81. if(flv_codecid == FLV_CODECID_VP6A)
  82. vcodec->codec_id = CODEC_ID_VP6A;
  83. if(vcodec->extradata_size != 1) {
  84. vcodec->extradata_size = 1;
  85. vcodec->extradata = av_malloc(1);
  86. }
  87. vcodec->extradata[0] = get_byte(s->pb);
  88. return 1; // 1 byte body size adjustment for flv_read_packet()
  89. case FLV_CODECID_H264:
  90. vcodec->codec_id = CODEC_ID_H264;
  91. return 3; // not 4, reading packet type will consume one byte
  92. default:
  93. av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
  94. vcodec->codec_tag = flv_codecid;
  95. }
  96. return 0;
  97. }
  98. static int amf_get_string(ByteIOContext *ioc, char *buffer, int buffsize) {
  99. int length = get_be16(ioc);
  100. if(length >= buffsize) {
  101. url_fskip(ioc, length);
  102. return -1;
  103. }
  104. get_buffer(ioc, buffer, length);
  105. buffer[length] = '\0';
  106. return length;
  107. }
  108. static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
  109. AVCodecContext *acodec, *vcodec;
  110. ByteIOContext *ioc;
  111. AMFDataType amf_type;
  112. char str_val[256];
  113. double num_val;
  114. num_val = 0;
  115. ioc = s->pb;
  116. amf_type = get_byte(ioc);
  117. switch(amf_type) {
  118. case AMF_DATA_TYPE_NUMBER:
  119. num_val = av_int2dbl(get_be64(ioc)); break;
  120. case AMF_DATA_TYPE_BOOL:
  121. num_val = get_byte(ioc); break;
  122. case AMF_DATA_TYPE_STRING:
  123. if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
  124. return -1;
  125. break;
  126. case AMF_DATA_TYPE_OBJECT: {
  127. unsigned int keylen;
  128. while(url_ftell(ioc) < max_pos - 2 && (keylen = get_be16(ioc))) {
  129. url_fskip(ioc, keylen); //skip key string
  130. if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
  131. return -1; //if we couldn't skip, bomb out.
  132. }
  133. if(get_byte(ioc) != AMF_END_OF_OBJECT)
  134. return -1;
  135. }
  136. break;
  137. case AMF_DATA_TYPE_NULL:
  138. case AMF_DATA_TYPE_UNDEFINED:
  139. case AMF_DATA_TYPE_UNSUPPORTED:
  140. break; //these take up no additional space
  141. case AMF_DATA_TYPE_MIXEDARRAY:
  142. url_fskip(ioc, 4); //skip 32-bit max array index
  143. while(url_ftell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
  144. //this is the only case in which we would want a nested parse to not skip over the object
  145. if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
  146. return -1;
  147. }
  148. if(get_byte(ioc) != AMF_END_OF_OBJECT)
  149. return -1;
  150. break;
  151. case AMF_DATA_TYPE_ARRAY: {
  152. unsigned int arraylen, i;
  153. arraylen = get_be32(ioc);
  154. for(i = 0; i < arraylen && url_ftell(ioc) < max_pos - 1; i++) {
  155. if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
  156. return -1; //if we couldn't skip, bomb out.
  157. }
  158. }
  159. break;
  160. case AMF_DATA_TYPE_DATE:
  161. url_fskip(ioc, 8 + 2); //timestamp (double) and UTC offset (int16)
  162. break;
  163. default: //unsupported type, we couldn't skip
  164. return -1;
  165. }
  166. if(depth == 1 && key) { //only look for metadata values when we are not nested and key != NULL
  167. acodec = astream ? astream->codec : NULL;
  168. vcodec = vstream ? vstream->codec : NULL;
  169. if(amf_type == AMF_DATA_TYPE_BOOL) {
  170. av_strlcpy(str_val, num_val > 0 ? "true" : "false", sizeof(str_val));
  171. av_metadata_set(&s->metadata, key, str_val);
  172. } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
  173. snprintf(str_val, sizeof(str_val), "%.f", num_val);
  174. av_metadata_set(&s->metadata, key, str_val);
  175. if(!strcmp(key, "duration")) s->duration = num_val * AV_TIME_BASE;
  176. else if(!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
  177. vcodec->bit_rate = num_val * 1024.0;
  178. } else if (amf_type == AMF_DATA_TYPE_STRING)
  179. av_metadata_set(&s->metadata, key, str_val);
  180. }
  181. return 0;
  182. }
  183. static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
  184. AMFDataType type;
  185. AVStream *stream, *astream, *vstream;
  186. ByteIOContext *ioc;
  187. int i;
  188. char buffer[11]; //only needs to hold the string "onMetaData". Anything longer is something we don't want.
  189. astream = NULL;
  190. vstream = NULL;
  191. ioc = s->pb;
  192. //first object needs to be "onMetaData" string
  193. type = get_byte(ioc);
  194. if(type != AMF_DATA_TYPE_STRING || amf_get_string(ioc, buffer, sizeof(buffer)) < 0 || strcmp(buffer, "onMetaData"))
  195. return -1;
  196. //find the streams now so that amf_parse_object doesn't need to do the lookup every time it is called.
  197. for(i = 0; i < s->nb_streams; i++) {
  198. stream = s->streams[i];
  199. if (stream->codec->codec_type == CODEC_TYPE_AUDIO) astream = stream;
  200. else if(stream->codec->codec_type == CODEC_TYPE_VIDEO) vstream = stream;
  201. }
  202. //parse the second object (we want a mixed array)
  203. if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
  204. return -1;
  205. return 0;
  206. }
  207. static AVStream *create_stream(AVFormatContext *s, int is_audio){
  208. AVStream *st = av_new_stream(s, is_audio);
  209. if (!st)
  210. return NULL;
  211. st->codec->codec_type = is_audio ? CODEC_TYPE_AUDIO : CODEC_TYPE_VIDEO;
  212. av_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
  213. return st;
  214. }
  215. static int flv_read_header(AVFormatContext *s,
  216. AVFormatParameters *ap)
  217. {
  218. int offset, flags;
  219. url_fskip(s->pb, 4);
  220. flags = get_byte(s->pb);
  221. /* old flvtool cleared this field */
  222. /* FIXME: better fix needed */
  223. if (!flags) {
  224. flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
  225. av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
  226. }
  227. if((flags & (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
  228. != (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
  229. s->ctx_flags |= AVFMTCTX_NOHEADER;
  230. if(flags & FLV_HEADER_FLAG_HASVIDEO){
  231. if(!create_stream(s, 0))
  232. return AVERROR(ENOMEM);
  233. }
  234. if(flags & FLV_HEADER_FLAG_HASAUDIO){
  235. if(!create_stream(s, 1))
  236. return AVERROR(ENOMEM);
  237. }
  238. offset = get_be32(s->pb);
  239. url_fseek(s->pb, offset, SEEK_SET);
  240. s->start_time = 0;
  241. return 0;
  242. }
  243. static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
  244. {
  245. av_free(st->codec->extradata);
  246. st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  247. if (!st->codec->extradata)
  248. return AVERROR(ENOMEM);
  249. st->codec->extradata_size = size;
  250. get_buffer(s->pb, st->codec->extradata, st->codec->extradata_size);
  251. return 0;
  252. }
  253. static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
  254. {
  255. FLVContext *flv = s->priv_data;
  256. int ret, i, type, size, flags, is_audio;
  257. int64_t next, pos;
  258. int64_t dts, pts = AV_NOPTS_VALUE;
  259. AVStream *st = NULL;
  260. for(;;){
  261. pos = url_ftell(s->pb);
  262. url_fskip(s->pb, 4); /* size of previous packet */
  263. type = get_byte(s->pb);
  264. size = get_be24(s->pb);
  265. dts = get_be24(s->pb);
  266. dts |= get_byte(s->pb) << 24;
  267. // av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, dts:%d\n", type, size, dts);
  268. if (url_feof(s->pb))
  269. return AVERROR_EOF;
  270. url_fskip(s->pb, 3); /* stream id, always 0 */
  271. flags = 0;
  272. if(size == 0)
  273. continue;
  274. next= size + url_ftell(s->pb);
  275. if (type == FLV_TAG_TYPE_AUDIO) {
  276. is_audio=1;
  277. flags = get_byte(s->pb);
  278. size--;
  279. } else if (type == FLV_TAG_TYPE_VIDEO) {
  280. is_audio=0;
  281. flags = get_byte(s->pb);
  282. size--;
  283. if ((flags & 0xf0) == 0x50) /* video info / command frame */
  284. goto skip;
  285. } else {
  286. if (type == FLV_TAG_TYPE_META && size > 13+1+4)
  287. flv_read_metabody(s, next);
  288. else /* skip packet */
  289. av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
  290. skip:
  291. url_fseek(s->pb, next, SEEK_SET);
  292. continue;
  293. }
  294. /* skip empty data packets */
  295. if (!size)
  296. continue;
  297. /* now find stream */
  298. for(i=0;i<s->nb_streams;i++) {
  299. st = s->streams[i];
  300. if (st->id == is_audio)
  301. break;
  302. }
  303. if(i == s->nb_streams){
  304. av_log(s, AV_LOG_ERROR, "invalid stream\n");
  305. st= create_stream(s, is_audio);
  306. s->ctx_flags &= ~AVFMTCTX_NOHEADER;
  307. }
  308. // av_log(s, AV_LOG_DEBUG, "%d %X %d \n", is_audio, flags, st->discard);
  309. if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio))
  310. ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
  311. || st->discard >= AVDISCARD_ALL
  312. ){
  313. url_fseek(s->pb, next, SEEK_SET);
  314. continue;
  315. }
  316. if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
  317. av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
  318. break;
  319. }
  320. // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps
  321. if(!url_is_streamed(s->pb) && s->duration==AV_NOPTS_VALUE){
  322. int size;
  323. const int64_t pos= url_ftell(s->pb);
  324. const int64_t fsize= url_fsize(s->pb);
  325. url_fseek(s->pb, fsize-4, SEEK_SET);
  326. size= get_be32(s->pb);
  327. url_fseek(s->pb, fsize-3-size, SEEK_SET);
  328. if(size == get_be24(s->pb) + 11){
  329. uint32_t ts = get_be24(s->pb);
  330. ts |= get_byte(s->pb) << 24;
  331. s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
  332. }
  333. url_fseek(s->pb, pos, SEEK_SET);
  334. }
  335. if(is_audio){
  336. if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
  337. st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
  338. st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
  339. st->codec->bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
  340. }
  341. if(!st->codec->codec_id){
  342. flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);
  343. }
  344. }else{
  345. size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
  346. }
  347. if (st->codec->codec_id == CODEC_ID_AAC ||
  348. st->codec->codec_id == CODEC_ID_H264) {
  349. int type = get_byte(s->pb);
  350. size--;
  351. if (st->codec->codec_id == CODEC_ID_H264) {
  352. int32_t cts = (get_be24(s->pb)+0xff800000)^0xff800000; // sign extension
  353. pts = dts + cts;
  354. if (cts < 0) { // dts are wrong
  355. flv->wrong_dts = 1;
  356. av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
  357. }
  358. if (flv->wrong_dts)
  359. dts = AV_NOPTS_VALUE;
  360. }
  361. if (type == 0) {
  362. if ((ret = flv_get_extradata(s, st, size)) < 0)
  363. return ret;
  364. if (st->codec->codec_id == CODEC_ID_AAC) {
  365. MPEG4AudioConfig cfg;
  366. ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
  367. st->codec->extradata_size);
  368. if (cfg.chan_config > 7)
  369. return -1;
  370. st->codec->channels = ff_mpeg4audio_channels[cfg.chan_config];
  371. st->codec->sample_rate = cfg.sample_rate;
  372. dprintf(s, "mp4a config channels %d sample rate %d\n",
  373. st->codec->channels, st->codec->sample_rate);
  374. }
  375. return AVERROR(EAGAIN);
  376. }
  377. }
  378. /* skip empty data packets */
  379. if (!size)
  380. return AVERROR(EAGAIN);
  381. ret= av_get_packet(s->pb, pkt, size);
  382. if (ret < 0) {
  383. return AVERROR(EIO);
  384. }
  385. /* note: we need to modify the packet size here to handle the last
  386. packet */
  387. pkt->size = ret;
  388. pkt->dts = dts;
  389. pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
  390. pkt->stream_index = st->index;
  391. if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
  392. pkt->flags |= PKT_FLAG_KEY;
  393. return ret;
  394. }
  395. AVInputFormat flv_demuxer = {
  396. "flv",
  397. NULL_IF_CONFIG_SMALL("FLV format"),
  398. sizeof(FLVContext),
  399. flv_probe,
  400. flv_read_header,
  401. flv_read_packet,
  402. .extensions = "flv",
  403. .value = CODEC_ID_FLV1,
  404. };