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.

783 lines
22KB

  1. /*
  2. * FFM (ffserver live feed) muxer and 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 <unistd.h>
  23. /* The FFM file is made of blocks of fixed size */
  24. #define FFM_HEADER_SIZE 14
  25. #define PACKET_ID 0x666d
  26. /* each packet contains frames (which can span several packets */
  27. #define FRAME_HEADER_SIZE 8
  28. #define FLAG_KEY_FRAME 0x01
  29. typedef struct FFMStream {
  30. int64_t pts;
  31. } FFMStream;
  32. enum {
  33. READ_HEADER,
  34. READ_DATA,
  35. };
  36. typedef struct FFMContext {
  37. /* only reading mode */
  38. offset_t write_index, file_size;
  39. int read_state;
  40. uint8_t header[FRAME_HEADER_SIZE];
  41. /* read and write */
  42. int first_packet; /* true if first packet, needed to set the discontinuity tag */
  43. int first_frame_in_packet; /* true if first frame in packet, needed to know if PTS information is valid */
  44. int packet_size;
  45. int frame_offset;
  46. int64_t pts;
  47. uint8_t *packet_ptr, *packet_end;
  48. uint8_t packet[FFM_PACKET_SIZE];
  49. } FFMContext;
  50. static int64_t get_pts(AVFormatContext *s, offset_t pos);
  51. /* disable pts hack for testing */
  52. int ffm_nopts = 0;
  53. #ifdef CONFIG_MUXERS
  54. static void flush_packet(AVFormatContext *s)
  55. {
  56. FFMContext *ffm = s->priv_data;
  57. int fill_size, h;
  58. ByteIOContext *pb = s->pb;
  59. fill_size = ffm->packet_end - ffm->packet_ptr;
  60. memset(ffm->packet_ptr, 0, fill_size);
  61. if (url_ftell(pb) % ffm->packet_size)
  62. av_abort();
  63. /* put header */
  64. put_be16(pb, PACKET_ID);
  65. put_be16(pb, fill_size);
  66. put_be64(pb, ffm->pts);
  67. h = ffm->frame_offset;
  68. if (ffm->first_packet)
  69. h |= 0x8000;
  70. put_be16(pb, h);
  71. put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
  72. put_flush_packet(pb);
  73. /* prepare next packet */
  74. ffm->frame_offset = 0; /* no key frame */
  75. ffm->pts = 0; /* no pts */
  76. ffm->packet_ptr = ffm->packet;
  77. ffm->first_packet = 0;
  78. }
  79. /* 'first' is true if first data of a frame */
  80. static void ffm_write_data(AVFormatContext *s,
  81. const uint8_t *buf, int size,
  82. int64_t pts, int first)
  83. {
  84. FFMContext *ffm = s->priv_data;
  85. int len;
  86. if (first && ffm->frame_offset == 0)
  87. ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
  88. if (first && ffm->pts == 0)
  89. ffm->pts = pts;
  90. /* write as many packets as needed */
  91. while (size > 0) {
  92. len = ffm->packet_end - ffm->packet_ptr;
  93. if (len > size)
  94. len = size;
  95. memcpy(ffm->packet_ptr, buf, len);
  96. ffm->packet_ptr += len;
  97. buf += len;
  98. size -= len;
  99. if (ffm->packet_ptr >= ffm->packet_end) {
  100. /* special case : no pts in packet : we leave the current one */
  101. if (ffm->pts == 0)
  102. ffm->pts = pts;
  103. flush_packet(s);
  104. }
  105. }
  106. }
  107. static int ffm_write_header(AVFormatContext *s)
  108. {
  109. FFMContext *ffm = s->priv_data;
  110. AVStream *st;
  111. FFMStream *fst;
  112. ByteIOContext *pb = s->pb;
  113. AVCodecContext *codec;
  114. int bit_rate, i;
  115. ffm->packet_size = FFM_PACKET_SIZE;
  116. /* header */
  117. put_le32(pb, MKTAG('F', 'F', 'M', '1'));
  118. put_be32(pb, ffm->packet_size);
  119. /* XXX: store write position in other file ? */
  120. put_be64(pb, ffm->packet_size); /* current write position */
  121. put_be32(pb, s->nb_streams);
  122. bit_rate = 0;
  123. for(i=0;i<s->nb_streams;i++) {
  124. st = s->streams[i];
  125. bit_rate += st->codec->bit_rate;
  126. }
  127. put_be32(pb, bit_rate);
  128. /* list of streams */
  129. for(i=0;i<s->nb_streams;i++) {
  130. st = s->streams[i];
  131. fst = av_mallocz(sizeof(FFMStream));
  132. if (!fst)
  133. goto fail;
  134. av_set_pts_info(st, 64, 1, 1000000);
  135. st->priv_data = fst;
  136. codec = st->codec;
  137. /* generic info */
  138. put_be32(pb, codec->codec_id);
  139. put_byte(pb, codec->codec_type);
  140. put_be32(pb, codec->bit_rate);
  141. put_be32(pb, st->quality);
  142. put_be32(pb, codec->flags);
  143. put_be32(pb, codec->flags2);
  144. put_be32(pb, codec->debug);
  145. /* specific info */
  146. switch(codec->codec_type) {
  147. case CODEC_TYPE_VIDEO:
  148. put_be32(pb, codec->time_base.num);
  149. put_be32(pb, codec->time_base.den);
  150. put_be16(pb, codec->width);
  151. put_be16(pb, codec->height);
  152. put_be16(pb, codec->gop_size);
  153. put_be32(pb, codec->pix_fmt);
  154. put_byte(pb, codec->qmin);
  155. put_byte(pb, codec->qmax);
  156. put_byte(pb, codec->max_qdiff);
  157. put_be16(pb, (int) (codec->qcompress * 10000.0));
  158. put_be16(pb, (int) (codec->qblur * 10000.0));
  159. put_be32(pb, codec->bit_rate_tolerance);
  160. put_strz(pb, codec->rc_eq);
  161. put_be32(pb, codec->rc_max_rate);
  162. put_be32(pb, codec->rc_min_rate);
  163. put_be32(pb, codec->rc_buffer_size);
  164. put_be64(pb, av_dbl2int(codec->i_quant_factor));
  165. put_be64(pb, av_dbl2int(codec->b_quant_factor));
  166. put_be64(pb, av_dbl2int(codec->i_quant_offset));
  167. put_be64(pb, av_dbl2int(codec->b_quant_offset));
  168. put_be32(pb, codec->dct_algo);
  169. put_be32(pb, codec->strict_std_compliance);
  170. put_be32(pb, codec->max_b_frames);
  171. put_be32(pb, codec->luma_elim_threshold);
  172. put_be32(pb, codec->chroma_elim_threshold);
  173. put_be32(pb, codec->mpeg_quant);
  174. put_be32(pb, codec->intra_dc_precision);
  175. put_be32(pb, codec->me_method);
  176. put_be32(pb, codec->mb_decision);
  177. put_be32(pb, codec->nsse_weight);
  178. put_be32(pb, codec->frame_skip_cmp);
  179. put_be64(pb, av_dbl2int(codec->rc_buffer_aggressivity));
  180. put_be32(pb, codec->codec_tag);
  181. break;
  182. case CODEC_TYPE_AUDIO:
  183. put_be32(pb, codec->sample_rate);
  184. put_le16(pb, codec->channels);
  185. put_le16(pb, codec->frame_size);
  186. break;
  187. default:
  188. return -1;
  189. }
  190. /* hack to have real time */
  191. if (ffm_nopts)
  192. fst->pts = 0;
  193. else
  194. fst->pts = av_gettime();
  195. }
  196. /* flush until end of block reached */
  197. while ((url_ftell(pb) % ffm->packet_size) != 0)
  198. put_byte(pb, 0);
  199. put_flush_packet(pb);
  200. /* init packet mux */
  201. ffm->packet_ptr = ffm->packet;
  202. ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
  203. assert(ffm->packet_end >= ffm->packet);
  204. ffm->frame_offset = 0;
  205. ffm->pts = 0;
  206. ffm->first_packet = 1;
  207. return 0;
  208. fail:
  209. for(i=0;i<s->nb_streams;i++) {
  210. st = s->streams[i];
  211. av_freep(&st->priv_data);
  212. }
  213. return -1;
  214. }
  215. static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
  216. {
  217. AVStream *st = s->streams[pkt->stream_index];
  218. FFMStream *fst = st->priv_data;
  219. int64_t pts;
  220. uint8_t header[FRAME_HEADER_SIZE];
  221. pts = fst->pts;
  222. /* packet size & key_frame */
  223. header[0] = pkt->stream_index;
  224. header[1] = 0;
  225. if (pkt->flags & PKT_FLAG_KEY)
  226. header[1] |= FLAG_KEY_FRAME;
  227. AV_WB24(header+2, pkt->size);
  228. AV_WB24(header+5, pkt->duration);
  229. ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
  230. ffm_write_data(s, pkt->data, pkt->size, pts, 0);
  231. fst->pts += pkt->duration;
  232. return 0;
  233. }
  234. static int ffm_write_trailer(AVFormatContext *s)
  235. {
  236. ByteIOContext *pb = s->pb;
  237. FFMContext *ffm = s->priv_data;
  238. /* flush packets */
  239. if (ffm->packet_ptr > ffm->packet)
  240. flush_packet(s);
  241. put_flush_packet(pb);
  242. if (!url_is_streamed(pb)) {
  243. int64_t size;
  244. /* update the write offset */
  245. size = url_ftell(pb);
  246. url_fseek(pb, 8, SEEK_SET);
  247. put_be64(pb, size);
  248. put_flush_packet(pb);
  249. }
  250. return 0;
  251. }
  252. #endif //CONFIG_MUXERS
  253. /* ffm demux */
  254. static int ffm_is_avail_data(AVFormatContext *s, int size)
  255. {
  256. FFMContext *ffm = s->priv_data;
  257. offset_t pos, avail_size;
  258. int len;
  259. len = ffm->packet_end - ffm->packet_ptr;
  260. if (!ffm_nopts) {
  261. /* XXX: I don't understand this test, so I disabled it for testing */
  262. if (size <= len)
  263. return 1;
  264. }
  265. pos = url_ftell(s->pb);
  266. if (pos == ffm->write_index) {
  267. /* exactly at the end of stream */
  268. return 0;
  269. } else if (pos < ffm->write_index) {
  270. avail_size = ffm->write_index - pos;
  271. } else {
  272. avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
  273. }
  274. avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
  275. if (size <= avail_size)
  276. return 1;
  277. else
  278. return 0;
  279. }
  280. /* first is true if we read the frame header */
  281. static int ffm_read_data(AVFormatContext *s,
  282. uint8_t *buf, int size, int first)
  283. {
  284. FFMContext *ffm = s->priv_data;
  285. ByteIOContext *pb = s->pb;
  286. int len, fill_size, size1, frame_offset;
  287. size1 = size;
  288. while (size > 0) {
  289. redo:
  290. len = ffm->packet_end - ffm->packet_ptr;
  291. if (len > size)
  292. len = size;
  293. if (len == 0) {
  294. if (url_ftell(pb) == ffm->file_size)
  295. url_fseek(pb, ffm->packet_size, SEEK_SET);
  296. retry_read:
  297. get_be16(pb); /* PACKET_ID */
  298. fill_size = get_be16(pb);
  299. ffm->pts = get_be64(pb);
  300. ffm->first_frame_in_packet = 1;
  301. frame_offset = get_be16(pb);
  302. get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
  303. ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
  304. if (ffm->packet_end < ffm->packet)
  305. return -1;
  306. /* if first packet or resynchronization packet, we must
  307. handle it specifically */
  308. if (ffm->first_packet || (frame_offset & 0x8000)) {
  309. if (!frame_offset) {
  310. /* This packet has no frame headers in it */
  311. if (url_ftell(pb) >= ffm->packet_size * 3) {
  312. url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
  313. goto retry_read;
  314. }
  315. /* This is bad, we cannot find a valid frame header */
  316. return 0;
  317. }
  318. ffm->first_packet = 0;
  319. if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
  320. return -1;
  321. ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
  322. if (!first)
  323. break;
  324. } else {
  325. ffm->packet_ptr = ffm->packet;
  326. }
  327. goto redo;
  328. }
  329. memcpy(buf, ffm->packet_ptr, len);
  330. buf += len;
  331. ffm->packet_ptr += len;
  332. size -= len;
  333. first = 0;
  334. }
  335. return size1 - size;
  336. }
  337. static void adjust_write_index(AVFormatContext *s)
  338. {
  339. FFMContext *ffm = s->priv_data;
  340. ByteIOContext *pb = s->pb;
  341. int64_t pts;
  342. //offset_t orig_write_index = ffm->write_index;
  343. offset_t pos_min, pos_max;
  344. int64_t pts_start;
  345. offset_t ptr = url_ftell(pb);
  346. pos_min = 0;
  347. pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
  348. pts_start = get_pts(s, pos_min);
  349. pts = get_pts(s, pos_max);
  350. if (pts - 100000 > pts_start)
  351. goto end;
  352. ffm->write_index = FFM_PACKET_SIZE;
  353. pts_start = get_pts(s, pos_min);
  354. pts = get_pts(s, pos_max);
  355. if (pts - 100000 <= pts_start) {
  356. while (1) {
  357. offset_t newpos;
  358. int64_t newpts;
  359. newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
  360. if (newpos == pos_min)
  361. break;
  362. newpts = get_pts(s, newpos);
  363. if (newpts - 100000 <= pts) {
  364. pos_max = newpos;
  365. pts = newpts;
  366. } else {
  367. pos_min = newpos;
  368. }
  369. }
  370. ffm->write_index += pos_max;
  371. }
  372. //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
  373. //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
  374. end:
  375. url_fseek(pb, ptr, SEEK_SET);
  376. }
  377. static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
  378. {
  379. FFMContext *ffm = s->priv_data;
  380. AVStream *st;
  381. FFMStream *fst;
  382. ByteIOContext *pb = s->pb;
  383. AVCodecContext *codec;
  384. int i, nb_streams;
  385. uint32_t tag;
  386. /* header */
  387. tag = get_le32(pb);
  388. if (tag != MKTAG('F', 'F', 'M', '1'))
  389. goto fail;
  390. ffm->packet_size = get_be32(pb);
  391. if (ffm->packet_size != FFM_PACKET_SIZE)
  392. goto fail;
  393. ffm->write_index = get_be64(pb);
  394. /* get also filesize */
  395. if (!url_is_streamed(pb)) {
  396. ffm->file_size = url_fsize(pb);
  397. adjust_write_index(s);
  398. } else {
  399. ffm->file_size = (UINT64_C(1) << 63) - 1;
  400. }
  401. nb_streams = get_be32(pb);
  402. get_be32(pb); /* total bitrate */
  403. /* read each stream */
  404. for(i=0;i<nb_streams;i++) {
  405. char rc_eq_buf[128];
  406. st = av_new_stream(s, 0);
  407. if (!st)
  408. goto fail;
  409. fst = av_mallocz(sizeof(FFMStream));
  410. if (!fst)
  411. goto fail;
  412. s->streams[i] = st;
  413. av_set_pts_info(st, 64, 1, 1000000);
  414. st->priv_data = fst;
  415. codec = st->codec;
  416. /* generic info */
  417. codec->codec_id = get_be32(pb);
  418. codec->codec_type = get_byte(pb); /* codec_type */
  419. codec->bit_rate = get_be32(pb);
  420. st->quality = get_be32(pb);
  421. codec->flags = get_be32(pb);
  422. codec->flags2 = get_be32(pb);
  423. codec->debug = get_be32(pb);
  424. /* specific info */
  425. switch(codec->codec_type) {
  426. case CODEC_TYPE_VIDEO:
  427. codec->time_base.num = get_be32(pb);
  428. codec->time_base.den = get_be32(pb);
  429. codec->width = get_be16(pb);
  430. codec->height = get_be16(pb);
  431. codec->gop_size = get_be16(pb);
  432. codec->pix_fmt = get_be32(pb);
  433. codec->qmin = get_byte(pb);
  434. codec->qmax = get_byte(pb);
  435. codec->max_qdiff = get_byte(pb);
  436. codec->qcompress = get_be16(pb) / 10000.0;
  437. codec->qblur = get_be16(pb) / 10000.0;
  438. codec->bit_rate_tolerance = get_be32(pb);
  439. codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
  440. codec->rc_max_rate = get_be32(pb);
  441. codec->rc_min_rate = get_be32(pb);
  442. codec->rc_buffer_size = get_be32(pb);
  443. codec->i_quant_factor = av_int2dbl(get_be64(pb));
  444. codec->b_quant_factor = av_int2dbl(get_be64(pb));
  445. codec->i_quant_offset = av_int2dbl(get_be64(pb));
  446. codec->b_quant_offset = av_int2dbl(get_be64(pb));
  447. codec->dct_algo = get_be32(pb);
  448. codec->strict_std_compliance = get_be32(pb);
  449. codec->max_b_frames = get_be32(pb);
  450. codec->luma_elim_threshold = get_be32(pb);
  451. codec->chroma_elim_threshold = get_be32(pb);
  452. codec->mpeg_quant = get_be32(pb);
  453. codec->intra_dc_precision = get_be32(pb);
  454. codec->me_method = get_be32(pb);
  455. codec->mb_decision = get_be32(pb);
  456. codec->nsse_weight = get_be32(pb);
  457. codec->frame_skip_cmp = get_be32(pb);
  458. codec->rc_buffer_aggressivity = av_int2dbl(get_be64(pb));
  459. codec->codec_tag = get_be32(pb);
  460. break;
  461. case CODEC_TYPE_AUDIO:
  462. codec->sample_rate = get_be32(pb);
  463. codec->channels = get_le16(pb);
  464. codec->frame_size = get_le16(pb);
  465. break;
  466. default:
  467. goto fail;
  468. }
  469. }
  470. /* get until end of block reached */
  471. while ((url_ftell(pb) % ffm->packet_size) != 0)
  472. get_byte(pb);
  473. /* init packet demux */
  474. ffm->packet_ptr = ffm->packet;
  475. ffm->packet_end = ffm->packet;
  476. ffm->frame_offset = 0;
  477. ffm->pts = 0;
  478. ffm->read_state = READ_HEADER;
  479. ffm->first_packet = 1;
  480. return 0;
  481. fail:
  482. for(i=0;i<s->nb_streams;i++) {
  483. st = s->streams[i];
  484. if (st) {
  485. av_freep(&st->priv_data);
  486. av_free(st);
  487. }
  488. }
  489. return -1;
  490. }
  491. /* return < 0 if eof */
  492. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  493. {
  494. int size;
  495. FFMContext *ffm = s->priv_data;
  496. int duration;
  497. switch(ffm->read_state) {
  498. case READ_HEADER:
  499. if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
  500. return AVERROR(EAGAIN);
  501. }
  502. #if 0
  503. printf("pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  504. url_ftell(s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
  505. #endif
  506. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  507. FRAME_HEADER_SIZE)
  508. return AVERROR(EAGAIN);
  509. #if 0
  510. {
  511. int i;
  512. for(i=0;i<FRAME_HEADER_SIZE;i++)
  513. printf("%02x ", ffm->header[i]);
  514. printf("\n");
  515. }
  516. #endif
  517. ffm->read_state = READ_DATA;
  518. /* fall thru */
  519. case READ_DATA:
  520. size = AV_RB24(ffm->header + 2);
  521. if (!ffm_is_avail_data(s, size)) {
  522. return AVERROR(EAGAIN);
  523. }
  524. duration = AV_RB24(ffm->header + 5);
  525. av_new_packet(pkt, size);
  526. pkt->stream_index = ffm->header[0];
  527. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  528. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  529. av_free_packet(pkt);
  530. ffm->read_state = READ_HEADER;
  531. return AVERROR(EAGAIN);
  532. }
  533. pkt->pos = url_ftell(s->pb);
  534. if (ffm->header[1] & FLAG_KEY_FRAME)
  535. pkt->flags |= PKT_FLAG_KEY;
  536. ffm->read_state = READ_HEADER;
  537. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  538. /* bad case: desynchronized packet. we cancel all the packet loading */
  539. av_free_packet(pkt);
  540. return AVERROR(EAGAIN);
  541. }
  542. if (ffm->first_frame_in_packet)
  543. {
  544. pkt->pts = ffm->pts;
  545. ffm->first_frame_in_packet = 0;
  546. }
  547. pkt->duration = duration;
  548. break;
  549. }
  550. return 0;
  551. }
  552. //#define DEBUG_SEEK
  553. /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
  554. by the write position inside this function */
  555. static void ffm_seek1(AVFormatContext *s, offset_t pos1)
  556. {
  557. FFMContext *ffm = s->priv_data;
  558. ByteIOContext *pb = s->pb;
  559. offset_t pos;
  560. pos = pos1 + ffm->write_index;
  561. if (pos >= ffm->file_size)
  562. pos -= (ffm->file_size - FFM_PACKET_SIZE);
  563. #ifdef DEBUG_SEEK
  564. printf("seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
  565. #endif
  566. url_fseek(pb, pos, SEEK_SET);
  567. }
  568. static int64_t get_pts(AVFormatContext *s, offset_t pos)
  569. {
  570. ByteIOContext *pb = s->pb;
  571. int64_t pts;
  572. ffm_seek1(s, pos);
  573. url_fskip(pb, 4);
  574. pts = get_be64(pb);
  575. #ifdef DEBUG_SEEK
  576. printf("pts=%0.6f\n", pts / 1000000.0);
  577. #endif
  578. return pts;
  579. }
  580. /* seek to a given time in the file. The file read pointer is
  581. positioned at or before pts. XXX: the following code is quite
  582. approximative */
  583. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  584. {
  585. FFMContext *ffm = s->priv_data;
  586. offset_t pos_min, pos_max, pos;
  587. int64_t pts_min, pts_max, pts;
  588. double pos1;
  589. #ifdef DEBUG_SEEK
  590. printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  591. #endif
  592. /* find the position using linear interpolation (better than
  593. dichotomy in typical cases) */
  594. pos_min = 0;
  595. pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
  596. while (pos_min <= pos_max) {
  597. pts_min = get_pts(s, pos_min);
  598. pts_max = get_pts(s, pos_max);
  599. /* linear interpolation */
  600. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  601. (double)(pts_max - pts_min);
  602. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  603. if (pos <= pos_min)
  604. pos = pos_min;
  605. else if (pos >= pos_max)
  606. pos = pos_max;
  607. pts = get_pts(s, pos);
  608. /* check if we are lucky */
  609. if (pts == wanted_pts) {
  610. goto found;
  611. } else if (pts > wanted_pts) {
  612. pos_max = pos - FFM_PACKET_SIZE;
  613. } else {
  614. pos_min = pos + FFM_PACKET_SIZE;
  615. }
  616. }
  617. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  618. if (pos > 0)
  619. pos -= FFM_PACKET_SIZE;
  620. found:
  621. ffm_seek1(s, pos);
  622. return 0;
  623. }
  624. #ifdef CONFIG_FFSERVER
  625. offset_t ffm_read_write_index(int fd)
  626. {
  627. uint8_t buf[8];
  628. lseek(fd, 8, SEEK_SET);
  629. read(fd, buf, 8);
  630. return AV_RB64(buf);
  631. }
  632. void ffm_write_write_index(int fd, offset_t pos)
  633. {
  634. uint8_t buf[8];
  635. int i;
  636. for(i=0;i<8;i++)
  637. buf[i] = (pos >> (56 - i * 8)) & 0xff;
  638. lseek(fd, 8, SEEK_SET);
  639. write(fd, buf, 8);
  640. }
  641. void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
  642. {
  643. FFMContext *ffm = s->priv_data;
  644. ffm->write_index = pos;
  645. ffm->file_size = file_size;
  646. }
  647. #endif // CONFIG_FFSERVER
  648. static int ffm_read_close(AVFormatContext *s)
  649. {
  650. AVStream *st;
  651. int i;
  652. for(i=0;i<s->nb_streams;i++) {
  653. st = s->streams[i];
  654. av_freep(&st->priv_data);
  655. }
  656. return 0;
  657. }
  658. static int ffm_probe(AVProbeData *p)
  659. {
  660. if (
  661. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  662. p->buf[3] == '1')
  663. return AVPROBE_SCORE_MAX + 1;
  664. return 0;
  665. }
  666. #ifdef CONFIG_FFM_DEMUXER
  667. AVInputFormat ffm_demuxer = {
  668. "ffm",
  669. "ffm format",
  670. sizeof(FFMContext),
  671. ffm_probe,
  672. ffm_read_header,
  673. ffm_read_packet,
  674. ffm_read_close,
  675. ffm_seek,
  676. };
  677. #endif
  678. #ifdef CONFIG_FFM_MUXER
  679. AVOutputFormat ffm_muxer = {
  680. "ffm",
  681. "ffm format",
  682. "",
  683. "ffm",
  684. sizeof(FFMContext),
  685. /* not really used */
  686. CODEC_ID_MP2,
  687. CODEC_ID_MPEG1VIDEO,
  688. ffm_write_header,
  689. ffm_write_packet,
  690. ffm_write_trailer,
  691. };
  692. #endif //CONFIG_FFM_MUXER