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.

482 lines
15KB

  1. /*
  2. * FFM (avserver live feed) demuxer
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/intreadwrite.h"
  22. #include "libavutil/intfloat.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "ffm.h"
  26. static int ffm_is_avail_data(AVFormatContext *s, int size)
  27. {
  28. FFMContext *ffm = s->priv_data;
  29. int64_t pos, avail_size;
  30. int len;
  31. len = ffm->packet_end - ffm->packet_ptr;
  32. if (size <= len)
  33. return 1;
  34. pos = avio_tell(s->pb);
  35. if (!ffm->write_index) {
  36. if (pos == ffm->file_size)
  37. return AVERROR_EOF;
  38. avail_size = ffm->file_size - pos;
  39. } else {
  40. if (pos == ffm->write_index) {
  41. /* exactly at the end of stream */
  42. return AVERROR(EAGAIN);
  43. } else if (pos < ffm->write_index) {
  44. avail_size = ffm->write_index - pos;
  45. } else {
  46. avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
  47. }
  48. }
  49. avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
  50. if (size <= avail_size)
  51. return 1;
  52. else
  53. return AVERROR(EAGAIN);
  54. }
  55. static int ffm_resync(AVFormatContext *s, int state)
  56. {
  57. av_log(s, AV_LOG_ERROR, "resyncing\n");
  58. while (state != PACKET_ID) {
  59. if (s->pb->eof_reached) {
  60. av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
  61. return -1;
  62. }
  63. state = (state << 8) | avio_r8(s->pb);
  64. }
  65. return 0;
  66. }
  67. /* first is true if we read the frame header */
  68. static int ffm_read_data(AVFormatContext *s,
  69. uint8_t *buf, int size, int header)
  70. {
  71. FFMContext *ffm = s->priv_data;
  72. AVIOContext *pb = s->pb;
  73. int len, fill_size, size1, frame_offset, id;
  74. size1 = size;
  75. while (size > 0) {
  76. redo:
  77. len = ffm->packet_end - ffm->packet_ptr;
  78. if (len < 0)
  79. return -1;
  80. if (len > size)
  81. len = size;
  82. if (len == 0) {
  83. if (avio_tell(pb) == ffm->file_size)
  84. avio_seek(pb, ffm->packet_size, SEEK_SET);
  85. retry_read:
  86. id = avio_rb16(pb); /* PACKET_ID */
  87. if (id != PACKET_ID)
  88. if (ffm_resync(s, id) < 0)
  89. return -1;
  90. fill_size = avio_rb16(pb);
  91. ffm->dts = avio_rb64(pb);
  92. frame_offset = avio_rb16(pb);
  93. avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
  94. ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
  95. if (ffm->packet_end < ffm->packet || frame_offset < 0)
  96. return -1;
  97. /* if first packet or resynchronization packet, we must
  98. handle it specifically */
  99. if (ffm->first_packet || (frame_offset & 0x8000)) {
  100. if (!frame_offset) {
  101. /* This packet has no frame headers in it */
  102. if (avio_tell(pb) >= ffm->packet_size * 3) {
  103. avio_seek(pb, -ffm->packet_size * 2, SEEK_CUR);
  104. goto retry_read;
  105. }
  106. /* This is bad, we cannot find a valid frame header */
  107. return 0;
  108. }
  109. ffm->first_packet = 0;
  110. if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
  111. return -1;
  112. ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
  113. if (!header)
  114. break;
  115. } else {
  116. ffm->packet_ptr = ffm->packet;
  117. }
  118. goto redo;
  119. }
  120. memcpy(buf, ffm->packet_ptr, len);
  121. buf += len;
  122. ffm->packet_ptr += len;
  123. size -= len;
  124. header = 0;
  125. }
  126. return size1 - size;
  127. }
  128. /* ensure that acutal seeking happens between FFM_PACKET_SIZE
  129. and file_size - FFM_PACKET_SIZE */
  130. static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
  131. {
  132. FFMContext *ffm = s->priv_data;
  133. AVIOContext *pb = s->pb;
  134. int64_t pos;
  135. pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
  136. pos = FFMAX(pos, FFM_PACKET_SIZE);
  137. av_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
  138. return avio_seek(pb, pos, SEEK_SET);
  139. }
  140. static int64_t get_dts(AVFormatContext *s, int64_t pos)
  141. {
  142. AVIOContext *pb = s->pb;
  143. int64_t dts;
  144. ffm_seek1(s, pos);
  145. avio_skip(pb, 4);
  146. dts = avio_rb64(pb);
  147. av_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
  148. return dts;
  149. }
  150. static void adjust_write_index(AVFormatContext *s)
  151. {
  152. FFMContext *ffm = s->priv_data;
  153. AVIOContext *pb = s->pb;
  154. int64_t pts;
  155. //int64_t orig_write_index = ffm->write_index;
  156. int64_t pos_min, pos_max;
  157. int64_t pts_start;
  158. int64_t ptr = avio_tell(pb);
  159. pos_min = 0;
  160. pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
  161. pts_start = get_dts(s, pos_min);
  162. pts = get_dts(s, pos_max);
  163. if (pts - 100000 > pts_start)
  164. goto end;
  165. ffm->write_index = FFM_PACKET_SIZE;
  166. pts_start = get_dts(s, pos_min);
  167. pts = get_dts(s, pos_max);
  168. if (pts - 100000 <= pts_start) {
  169. while (1) {
  170. int64_t newpos;
  171. int64_t newpts;
  172. newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
  173. if (newpos == pos_min)
  174. break;
  175. newpts = get_dts(s, newpos);
  176. if (newpts - 100000 <= pts) {
  177. pos_max = newpos;
  178. pts = newpts;
  179. } else {
  180. pos_min = newpos;
  181. }
  182. }
  183. ffm->write_index += pos_max;
  184. }
  185. end:
  186. avio_seek(pb, ptr, SEEK_SET);
  187. }
  188. static int ffm_close(AVFormatContext *s)
  189. {
  190. int i;
  191. for (i = 0; i < s->nb_streams; i++)
  192. av_freep(&s->streams[i]->codec->rc_eq);
  193. return 0;
  194. }
  195. static int ffm_read_header(AVFormatContext *s)
  196. {
  197. FFMContext *ffm = s->priv_data;
  198. AVStream *st;
  199. AVIOContext *pb = s->pb;
  200. AVCodecContext *codec;
  201. int i, nb_streams;
  202. uint32_t tag;
  203. /* header */
  204. tag = avio_rl32(pb);
  205. if (tag != MKTAG('F', 'F', 'M', '1'))
  206. goto fail;
  207. ffm->packet_size = avio_rb32(pb);
  208. if (ffm->packet_size != FFM_PACKET_SIZE)
  209. goto fail;
  210. ffm->write_index = avio_rb64(pb);
  211. /* get also filesize */
  212. if (pb->seekable) {
  213. ffm->file_size = avio_size(pb);
  214. if (ffm->write_index)
  215. adjust_write_index(s);
  216. } else {
  217. ffm->file_size = (UINT64_C(1) << 63) - 1;
  218. }
  219. nb_streams = avio_rb32(pb);
  220. avio_rb32(pb); /* total bitrate */
  221. /* read each stream */
  222. for(i=0;i<nb_streams;i++) {
  223. char rc_eq_buf[128];
  224. st = avformat_new_stream(s, NULL);
  225. if (!st)
  226. goto fail;
  227. avpriv_set_pts_info(st, 64, 1, 1000000);
  228. codec = st->codec;
  229. /* generic info */
  230. codec->codec_id = avio_rb32(pb);
  231. codec->codec_type = avio_r8(pb); /* codec_type */
  232. codec->bit_rate = avio_rb32(pb);
  233. codec->flags = avio_rb32(pb);
  234. codec->flags2 = avio_rb32(pb);
  235. codec->debug = avio_rb32(pb);
  236. /* specific info */
  237. switch(codec->codec_type) {
  238. case AVMEDIA_TYPE_VIDEO:
  239. codec->time_base.num = avio_rb32(pb);
  240. codec->time_base.den = avio_rb32(pb);
  241. codec->width = avio_rb16(pb);
  242. codec->height = avio_rb16(pb);
  243. codec->gop_size = avio_rb16(pb);
  244. codec->pix_fmt = avio_rb32(pb);
  245. codec->qmin = avio_r8(pb);
  246. codec->qmax = avio_r8(pb);
  247. codec->max_qdiff = avio_r8(pb);
  248. codec->qcompress = avio_rb16(pb) / 10000.0;
  249. codec->qblur = avio_rb16(pb) / 10000.0;
  250. codec->bit_rate_tolerance = avio_rb32(pb);
  251. avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  252. codec->rc_eq = av_strdup(rc_eq_buf);
  253. codec->rc_max_rate = avio_rb32(pb);
  254. codec->rc_min_rate = avio_rb32(pb);
  255. codec->rc_buffer_size = avio_rb32(pb);
  256. codec->i_quant_factor = av_int2double(avio_rb64(pb));
  257. codec->b_quant_factor = av_int2double(avio_rb64(pb));
  258. codec->i_quant_offset = av_int2double(avio_rb64(pb));
  259. codec->b_quant_offset = av_int2double(avio_rb64(pb));
  260. codec->dct_algo = avio_rb32(pb);
  261. codec->strict_std_compliance = avio_rb32(pb);
  262. codec->max_b_frames = avio_rb32(pb);
  263. codec->mpeg_quant = avio_rb32(pb);
  264. codec->intra_dc_precision = avio_rb32(pb);
  265. codec->me_method = avio_rb32(pb);
  266. codec->mb_decision = avio_rb32(pb);
  267. codec->nsse_weight = avio_rb32(pb);
  268. codec->frame_skip_cmp = avio_rb32(pb);
  269. codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
  270. codec->codec_tag = avio_rb32(pb);
  271. codec->thread_count = avio_r8(pb);
  272. codec->coder_type = avio_rb32(pb);
  273. codec->me_cmp = avio_rb32(pb);
  274. codec->me_subpel_quality = avio_rb32(pb);
  275. codec->me_range = avio_rb32(pb);
  276. codec->keyint_min = avio_rb32(pb);
  277. codec->scenechange_threshold = avio_rb32(pb);
  278. codec->b_frame_strategy = avio_rb32(pb);
  279. codec->qcompress = av_int2double(avio_rb64(pb));
  280. codec->qblur = av_int2double(avio_rb64(pb));
  281. codec->max_qdiff = avio_rb32(pb);
  282. codec->refs = avio_rb32(pb);
  283. break;
  284. case AVMEDIA_TYPE_AUDIO:
  285. codec->sample_rate = avio_rb32(pb);
  286. codec->channels = avio_rl16(pb);
  287. codec->frame_size = avio_rl16(pb);
  288. break;
  289. default:
  290. goto fail;
  291. }
  292. if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
  293. codec->extradata_size = avio_rb32(pb);
  294. codec->extradata = av_malloc(codec->extradata_size);
  295. if (!codec->extradata)
  296. return AVERROR(ENOMEM);
  297. avio_read(pb, codec->extradata, codec->extradata_size);
  298. }
  299. }
  300. /* get until end of block reached */
  301. while ((avio_tell(pb) % ffm->packet_size) != 0)
  302. avio_r8(pb);
  303. /* init packet demux */
  304. ffm->packet_ptr = ffm->packet;
  305. ffm->packet_end = ffm->packet;
  306. ffm->frame_offset = 0;
  307. ffm->dts = 0;
  308. ffm->read_state = READ_HEADER;
  309. ffm->first_packet = 1;
  310. return 0;
  311. fail:
  312. ffm_close(s);
  313. return -1;
  314. }
  315. /* return < 0 if eof */
  316. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  317. {
  318. int size;
  319. FFMContext *ffm = s->priv_data;
  320. int duration, ret;
  321. switch(ffm->read_state) {
  322. case READ_HEADER:
  323. if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
  324. return ret;
  325. av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  326. avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
  327. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  328. FRAME_HEADER_SIZE)
  329. return -1;
  330. if (ffm->header[1] & FLAG_DTS)
  331. if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
  332. return -1;
  333. ffm->read_state = READ_DATA;
  334. /* fall thru */
  335. case READ_DATA:
  336. size = AV_RB24(ffm->header + 2);
  337. if ((ret = ffm_is_avail_data(s, size)) < 0)
  338. return ret;
  339. duration = AV_RB24(ffm->header + 5);
  340. av_new_packet(pkt, size);
  341. pkt->stream_index = ffm->header[0];
  342. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  343. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  344. av_free_packet(pkt);
  345. ffm->read_state = READ_HEADER;
  346. return -1;
  347. }
  348. pkt->pos = avio_tell(s->pb);
  349. if (ffm->header[1] & FLAG_KEY_FRAME)
  350. pkt->flags |= AV_PKT_FLAG_KEY;
  351. ffm->read_state = READ_HEADER;
  352. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  353. /* bad case: desynchronized packet. we cancel all the packet loading */
  354. av_free_packet(pkt);
  355. return -1;
  356. }
  357. pkt->pts = AV_RB64(ffm->header+8);
  358. if (ffm->header[1] & FLAG_DTS)
  359. pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
  360. else
  361. pkt->dts = pkt->pts;
  362. pkt->duration = duration;
  363. break;
  364. }
  365. return 0;
  366. }
  367. /* seek to a given time in the file. The file read pointer is
  368. positioned at or before pts. XXX: the following code is quite
  369. approximative */
  370. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  371. {
  372. FFMContext *ffm = s->priv_data;
  373. int64_t pos_min, pos_max, pos;
  374. int64_t pts_min, pts_max, pts;
  375. double pos1;
  376. av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  377. /* find the position using linear interpolation (better than
  378. dichotomy in typical cases) */
  379. pos_min = FFM_PACKET_SIZE;
  380. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  381. while (pos_min <= pos_max) {
  382. pts_min = get_dts(s, pos_min);
  383. pts_max = get_dts(s, pos_max);
  384. /* linear interpolation */
  385. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  386. (double)(pts_max - pts_min);
  387. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  388. if (pos <= pos_min)
  389. pos = pos_min;
  390. else if (pos >= pos_max)
  391. pos = pos_max;
  392. pts = get_dts(s, pos);
  393. /* check if we are lucky */
  394. if (pts == wanted_pts) {
  395. goto found;
  396. } else if (pts > wanted_pts) {
  397. pos_max = pos - FFM_PACKET_SIZE;
  398. } else {
  399. pos_min = pos + FFM_PACKET_SIZE;
  400. }
  401. }
  402. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  403. found:
  404. if (ffm_seek1(s, pos) < 0)
  405. return -1;
  406. /* reset read state */
  407. ffm->read_state = READ_HEADER;
  408. ffm->packet_ptr = ffm->packet;
  409. ffm->packet_end = ffm->packet;
  410. ffm->first_packet = 1;
  411. return 0;
  412. }
  413. static int ffm_probe(AVProbeData *p)
  414. {
  415. if (
  416. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  417. p->buf[3] == '1')
  418. return AVPROBE_SCORE_MAX + 1;
  419. return 0;
  420. }
  421. AVInputFormat ff_ffm_demuxer = {
  422. .name = "ffm",
  423. .long_name = NULL_IF_CONFIG_SMALL("FFM (AVserver live feed)"),
  424. .priv_data_size = sizeof(FFMContext),
  425. .read_probe = ffm_probe,
  426. .read_header = ffm_read_header,
  427. .read_packet = ffm_read_packet,
  428. .read_close = ffm_close,
  429. .read_seek = ffm_seek,
  430. };