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.

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