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.

784 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. int size= pkt->size;
  222. pts = fst->pts;
  223. /* packet size & key_frame */
  224. header[0] = pkt->stream_index;
  225. header[1] = 0;
  226. if (pkt->flags & PKT_FLAG_KEY)
  227. header[1] |= FLAG_KEY_FRAME;
  228. AV_WB24(header+2, size);
  229. AV_WB24(header+5, pkt->duration);
  230. ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
  231. ffm_write_data(s, pkt->data, size, pts, 0);
  232. fst->pts += pkt->duration;
  233. return 0;
  234. }
  235. static int ffm_write_trailer(AVFormatContext *s)
  236. {
  237. ByteIOContext *pb = s->pb;
  238. FFMContext *ffm = s->priv_data;
  239. /* flush packets */
  240. if (ffm->packet_ptr > ffm->packet)
  241. flush_packet(s);
  242. put_flush_packet(pb);
  243. if (!url_is_streamed(pb)) {
  244. int64_t size;
  245. /* update the write offset */
  246. size = url_ftell(pb);
  247. url_fseek(pb, 8, SEEK_SET);
  248. put_be64(pb, size);
  249. put_flush_packet(pb);
  250. }
  251. return 0;
  252. }
  253. #endif //CONFIG_MUXERS
  254. /* ffm demux */
  255. static int ffm_is_avail_data(AVFormatContext *s, int size)
  256. {
  257. FFMContext *ffm = s->priv_data;
  258. offset_t pos, avail_size;
  259. int len;
  260. len = ffm->packet_end - ffm->packet_ptr;
  261. if (!ffm_nopts) {
  262. /* XXX: I don't understand this test, so I disabled it for testing */
  263. if (size <= len)
  264. return 1;
  265. }
  266. pos = url_ftell(s->pb);
  267. if (pos == ffm->write_index) {
  268. /* exactly at the end of stream */
  269. return 0;
  270. } else if (pos < ffm->write_index) {
  271. avail_size = ffm->write_index - pos;
  272. } else {
  273. avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
  274. }
  275. avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
  276. if (size <= avail_size)
  277. return 1;
  278. else
  279. return 0;
  280. }
  281. /* first is true if we read the frame header */
  282. static int ffm_read_data(AVFormatContext *s,
  283. uint8_t *buf, int size, int first)
  284. {
  285. FFMContext *ffm = s->priv_data;
  286. ByteIOContext *pb = s->pb;
  287. int len, fill_size, size1, frame_offset;
  288. size1 = size;
  289. while (size > 0) {
  290. redo:
  291. len = ffm->packet_end - ffm->packet_ptr;
  292. if (len > size)
  293. len = size;
  294. if (len == 0) {
  295. if (url_ftell(pb) == ffm->file_size)
  296. url_fseek(pb, ffm->packet_size, SEEK_SET);
  297. retry_read:
  298. get_be16(pb); /* PACKET_ID */
  299. fill_size = get_be16(pb);
  300. ffm->pts = get_be64(pb);
  301. ffm->first_frame_in_packet = 1;
  302. frame_offset = get_be16(pb);
  303. get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
  304. ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
  305. if (ffm->packet_end < ffm->packet)
  306. return -1;
  307. /* if first packet or resynchronization packet, we must
  308. handle it specifically */
  309. if (ffm->first_packet || (frame_offset & 0x8000)) {
  310. if (!frame_offset) {
  311. /* This packet has no frame headers in it */
  312. if (url_ftell(pb) >= ffm->packet_size * 3) {
  313. url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
  314. goto retry_read;
  315. }
  316. /* This is bad, we cannot find a valid frame header */
  317. return 0;
  318. }
  319. ffm->first_packet = 0;
  320. if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
  321. return -1;
  322. ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
  323. if (!first)
  324. break;
  325. } else {
  326. ffm->packet_ptr = ffm->packet;
  327. }
  328. goto redo;
  329. }
  330. memcpy(buf, ffm->packet_ptr, len);
  331. buf += len;
  332. ffm->packet_ptr += len;
  333. size -= len;
  334. first = 0;
  335. }
  336. return size1 - size;
  337. }
  338. static void adjust_write_index(AVFormatContext *s)
  339. {
  340. FFMContext *ffm = s->priv_data;
  341. ByteIOContext *pb = s->pb;
  342. int64_t pts;
  343. //offset_t orig_write_index = ffm->write_index;
  344. offset_t pos_min, pos_max;
  345. int64_t pts_start;
  346. offset_t ptr = url_ftell(pb);
  347. pos_min = 0;
  348. pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
  349. pts_start = get_pts(s, pos_min);
  350. pts = get_pts(s, pos_max);
  351. if (pts - 100000 > pts_start)
  352. goto end;
  353. ffm->write_index = FFM_PACKET_SIZE;
  354. pts_start = get_pts(s, pos_min);
  355. pts = get_pts(s, pos_max);
  356. if (pts - 100000 <= pts_start) {
  357. while (1) {
  358. offset_t newpos;
  359. int64_t newpts;
  360. newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
  361. if (newpos == pos_min)
  362. break;
  363. newpts = get_pts(s, newpos);
  364. if (newpts - 100000 <= pts) {
  365. pos_max = newpos;
  366. pts = newpts;
  367. } else {
  368. pos_min = newpos;
  369. }
  370. }
  371. ffm->write_index += pos_max;
  372. }
  373. //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
  374. //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
  375. end:
  376. url_fseek(pb, ptr, SEEK_SET);
  377. }
  378. static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
  379. {
  380. FFMContext *ffm = s->priv_data;
  381. AVStream *st;
  382. FFMStream *fst;
  383. ByteIOContext *pb = s->pb;
  384. AVCodecContext *codec;
  385. int i, nb_streams;
  386. uint32_t tag;
  387. /* header */
  388. tag = get_le32(pb);
  389. if (tag != MKTAG('F', 'F', 'M', '1'))
  390. goto fail;
  391. ffm->packet_size = get_be32(pb);
  392. if (ffm->packet_size != FFM_PACKET_SIZE)
  393. goto fail;
  394. ffm->write_index = get_be64(pb);
  395. /* get also filesize */
  396. if (!url_is_streamed(pb)) {
  397. ffm->file_size = url_fsize(pb);
  398. adjust_write_index(s);
  399. } else {
  400. ffm->file_size = (UINT64_C(1) << 63) - 1;
  401. }
  402. nb_streams = get_be32(pb);
  403. get_be32(pb); /* total bitrate */
  404. /* read each stream */
  405. for(i=0;i<nb_streams;i++) {
  406. char rc_eq_buf[128];
  407. st = av_new_stream(s, 0);
  408. if (!st)
  409. goto fail;
  410. fst = av_mallocz(sizeof(FFMStream));
  411. if (!fst)
  412. goto fail;
  413. s->streams[i] = st;
  414. av_set_pts_info(st, 64, 1, 1000000);
  415. st->priv_data = fst;
  416. codec = st->codec;
  417. /* generic info */
  418. codec->codec_id = get_be32(pb);
  419. codec->codec_type = get_byte(pb); /* codec_type */
  420. codec->bit_rate = get_be32(pb);
  421. st->quality = get_be32(pb);
  422. codec->flags = get_be32(pb);
  423. codec->flags2 = get_be32(pb);
  424. codec->debug = get_be32(pb);
  425. /* specific info */
  426. switch(codec->codec_type) {
  427. case CODEC_TYPE_VIDEO:
  428. codec->time_base.num = get_be32(pb);
  429. codec->time_base.den = get_be32(pb);
  430. codec->width = get_be16(pb);
  431. codec->height = get_be16(pb);
  432. codec->gop_size = get_be16(pb);
  433. codec->pix_fmt = get_be32(pb);
  434. codec->qmin = get_byte(pb);
  435. codec->qmax = get_byte(pb);
  436. codec->max_qdiff = get_byte(pb);
  437. codec->qcompress = get_be16(pb) / 10000.0;
  438. codec->qblur = get_be16(pb) / 10000.0;
  439. codec->bit_rate_tolerance = get_be32(pb);
  440. codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
  441. codec->rc_max_rate = get_be32(pb);
  442. codec->rc_min_rate = get_be32(pb);
  443. codec->rc_buffer_size = get_be32(pb);
  444. codec->i_quant_factor = av_int2dbl(get_be64(pb));
  445. codec->b_quant_factor = av_int2dbl(get_be64(pb));
  446. codec->i_quant_offset = av_int2dbl(get_be64(pb));
  447. codec->b_quant_offset = av_int2dbl(get_be64(pb));
  448. codec->dct_algo = get_be32(pb);
  449. codec->strict_std_compliance = get_be32(pb);
  450. codec->max_b_frames = get_be32(pb);
  451. codec->luma_elim_threshold = get_be32(pb);
  452. codec->chroma_elim_threshold = get_be32(pb);
  453. codec->mpeg_quant = get_be32(pb);
  454. codec->intra_dc_precision = get_be32(pb);
  455. codec->me_method = get_be32(pb);
  456. codec->mb_decision = get_be32(pb);
  457. codec->nsse_weight = get_be32(pb);
  458. codec->frame_skip_cmp = get_be32(pb);
  459. codec->rc_buffer_aggressivity = av_int2dbl(get_be64(pb));
  460. codec->codec_tag = get_be32(pb);
  461. break;
  462. case CODEC_TYPE_AUDIO:
  463. codec->sample_rate = get_be32(pb);
  464. codec->channels = get_le16(pb);
  465. codec->frame_size = get_le16(pb);
  466. break;
  467. default:
  468. goto fail;
  469. }
  470. }
  471. /* get until end of block reached */
  472. while ((url_ftell(pb) % ffm->packet_size) != 0)
  473. get_byte(pb);
  474. /* init packet demux */
  475. ffm->packet_ptr = ffm->packet;
  476. ffm->packet_end = ffm->packet;
  477. ffm->frame_offset = 0;
  478. ffm->pts = 0;
  479. ffm->read_state = READ_HEADER;
  480. ffm->first_packet = 1;
  481. return 0;
  482. fail:
  483. for(i=0;i<s->nb_streams;i++) {
  484. st = s->streams[i];
  485. if (st) {
  486. av_freep(&st->priv_data);
  487. av_free(st);
  488. }
  489. }
  490. return -1;
  491. }
  492. /* return < 0 if eof */
  493. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  494. {
  495. int size;
  496. FFMContext *ffm = s->priv_data;
  497. int duration;
  498. switch(ffm->read_state) {
  499. case READ_HEADER:
  500. if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
  501. return AVERROR(EAGAIN);
  502. }
  503. #if 0
  504. printf("pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  505. url_ftell(s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
  506. #endif
  507. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  508. FRAME_HEADER_SIZE)
  509. return AVERROR(EAGAIN);
  510. #if 0
  511. {
  512. int i;
  513. for(i=0;i<FRAME_HEADER_SIZE;i++)
  514. printf("%02x ", ffm->header[i]);
  515. printf("\n");
  516. }
  517. #endif
  518. ffm->read_state = READ_DATA;
  519. /* fall thru */
  520. case READ_DATA:
  521. size = AV_RB24(ffm->header + 2);
  522. if (!ffm_is_avail_data(s, size)) {
  523. return AVERROR(EAGAIN);
  524. }
  525. duration = AV_RB24(ffm->header + 5);
  526. av_new_packet(pkt, size);
  527. pkt->stream_index = ffm->header[0];
  528. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  529. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  530. av_free_packet(pkt);
  531. ffm->read_state = READ_HEADER;
  532. return AVERROR(EAGAIN);
  533. }
  534. pkt->pos = url_ftell(s->pb);
  535. if (ffm->header[1] & FLAG_KEY_FRAME)
  536. pkt->flags |= PKT_FLAG_KEY;
  537. ffm->read_state = READ_HEADER;
  538. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  539. /* bad case: desynchronized packet. we cancel all the packet loading */
  540. av_free_packet(pkt);
  541. return AVERROR(EAGAIN);
  542. }
  543. if (ffm->first_frame_in_packet)
  544. {
  545. pkt->pts = ffm->pts;
  546. ffm->first_frame_in_packet = 0;
  547. }
  548. pkt->duration = duration;
  549. break;
  550. }
  551. return 0;
  552. }
  553. //#define DEBUG_SEEK
  554. /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
  555. by the write position inside this function */
  556. static void ffm_seek1(AVFormatContext *s, offset_t pos1)
  557. {
  558. FFMContext *ffm = s->priv_data;
  559. ByteIOContext *pb = s->pb;
  560. offset_t pos;
  561. pos = pos1 + ffm->write_index;
  562. if (pos >= ffm->file_size)
  563. pos -= (ffm->file_size - FFM_PACKET_SIZE);
  564. #ifdef DEBUG_SEEK
  565. printf("seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
  566. #endif
  567. url_fseek(pb, pos, SEEK_SET);
  568. }
  569. static int64_t get_pts(AVFormatContext *s, offset_t pos)
  570. {
  571. ByteIOContext *pb = s->pb;
  572. int64_t pts;
  573. ffm_seek1(s, pos);
  574. url_fskip(pb, 4);
  575. pts = get_be64(pb);
  576. #ifdef DEBUG_SEEK
  577. printf("pts=%0.6f\n", pts / 1000000.0);
  578. #endif
  579. return pts;
  580. }
  581. /* seek to a given time in the file. The file read pointer is
  582. positioned at or before pts. XXX: the following code is quite
  583. approximative */
  584. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  585. {
  586. FFMContext *ffm = s->priv_data;
  587. offset_t pos_min, pos_max, pos;
  588. int64_t pts_min, pts_max, pts;
  589. double pos1;
  590. #ifdef DEBUG_SEEK
  591. printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  592. #endif
  593. /* find the position using linear interpolation (better than
  594. dichotomy in typical cases) */
  595. pos_min = 0;
  596. pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
  597. while (pos_min <= pos_max) {
  598. pts_min = get_pts(s, pos_min);
  599. pts_max = get_pts(s, pos_max);
  600. /* linear interpolation */
  601. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  602. (double)(pts_max - pts_min);
  603. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  604. if (pos <= pos_min)
  605. pos = pos_min;
  606. else if (pos >= pos_max)
  607. pos = pos_max;
  608. pts = get_pts(s, pos);
  609. /* check if we are lucky */
  610. if (pts == wanted_pts) {
  611. goto found;
  612. } else if (pts > wanted_pts) {
  613. pos_max = pos - FFM_PACKET_SIZE;
  614. } else {
  615. pos_min = pos + FFM_PACKET_SIZE;
  616. }
  617. }
  618. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  619. if (pos > 0)
  620. pos -= FFM_PACKET_SIZE;
  621. found:
  622. ffm_seek1(s, pos);
  623. return 0;
  624. }
  625. #ifdef CONFIG_FFSERVER
  626. offset_t ffm_read_write_index(int fd)
  627. {
  628. uint8_t buf[8];
  629. lseek(fd, 8, SEEK_SET);
  630. read(fd, buf, 8);
  631. return AV_RB64(buf);
  632. }
  633. void ffm_write_write_index(int fd, offset_t pos)
  634. {
  635. uint8_t buf[8];
  636. int i;
  637. for(i=0;i<8;i++)
  638. buf[i] = (pos >> (56 - i * 8)) & 0xff;
  639. lseek(fd, 8, SEEK_SET);
  640. write(fd, buf, 8);
  641. }
  642. void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
  643. {
  644. FFMContext *ffm = s->priv_data;
  645. ffm->write_index = pos;
  646. ffm->file_size = file_size;
  647. }
  648. #endif // CONFIG_FFSERVER
  649. static int ffm_read_close(AVFormatContext *s)
  650. {
  651. AVStream *st;
  652. int i;
  653. for(i=0;i<s->nb_streams;i++) {
  654. st = s->streams[i];
  655. av_freep(&st->priv_data);
  656. }
  657. return 0;
  658. }
  659. static int ffm_probe(AVProbeData *p)
  660. {
  661. if (
  662. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  663. p->buf[3] == '1')
  664. return AVPROBE_SCORE_MAX + 1;
  665. return 0;
  666. }
  667. #ifdef CONFIG_FFM_DEMUXER
  668. AVInputFormat ffm_demuxer = {
  669. "ffm",
  670. "ffm format",
  671. sizeof(FFMContext),
  672. ffm_probe,
  673. ffm_read_header,
  674. ffm_read_packet,
  675. ffm_read_close,
  676. ffm_seek,
  677. };
  678. #endif
  679. #ifdef CONFIG_FFM_MUXER
  680. AVOutputFormat ffm_muxer = {
  681. "ffm",
  682. "ffm format",
  683. "",
  684. "ffm",
  685. sizeof(FFMContext),
  686. /* not really used */
  687. CODEC_ID_MP2,
  688. CODEC_ID_MPEG1VIDEO,
  689. ffm_write_header,
  690. ffm_write_packet,
  691. ffm_write_trailer,
  692. };
  693. #endif //CONFIG_FFM_MUXER