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.

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