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.

504 lines
16KB

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