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
14KB

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