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.

498 lines
15KB

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