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.

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