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.

492 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 header)
  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 & 0x7fff) < FFM_HEADER_SIZE)
  113. return -1;
  114. ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
  115. if (!header)
  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. header = 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. av_log(s, AV_LOG_DEBUG, "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. av_log(s, AV_LOG_DEBUG, "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. ByteIOContext *pb = s->pb;
  203. AVCodecContext *codec;
  204. int i, nb_streams;
  205. uint32_t tag;
  206. /* header */
  207. tag = get_le32(pb);
  208. if (tag != MKTAG('F', 'F', 'M', '1'))
  209. goto fail;
  210. ffm->packet_size = get_be32(pb);
  211. if (ffm->packet_size != FFM_PACKET_SIZE)
  212. goto fail;
  213. ffm->write_index = get_be64(pb);
  214. /* get also filesize */
  215. if (!url_is_streamed(pb)) {
  216. ffm->file_size = url_fsize(pb);
  217. adjust_write_index(s);
  218. } else {
  219. ffm->file_size = (UINT64_C(1) << 63) - 1;
  220. }
  221. nb_streams = get_be32(pb);
  222. get_be32(pb); /* total bitrate */
  223. /* read each stream */
  224. for(i=0;i<nb_streams;i++) {
  225. char rc_eq_buf[128];
  226. st = av_new_stream(s, 0);
  227. if (!st)
  228. goto fail;
  229. s->streams[i] = st;
  230. av_set_pts_info(st, 64, 1, 1000000);
  231. codec = st->codec;
  232. /* generic info */
  233. codec->codec_id = get_be32(pb);
  234. codec->codec_type = get_byte(pb); /* codec_type */
  235. codec->bit_rate = get_be32(pb);
  236. st->quality = get_be32(pb);
  237. codec->flags = get_be32(pb);
  238. codec->flags2 = get_be32(pb);
  239. codec->debug = get_be32(pb);
  240. /* specific info */
  241. switch(codec->codec_type) {
  242. case CODEC_TYPE_VIDEO:
  243. codec->time_base.num = get_be32(pb);
  244. codec->time_base.den = get_be32(pb);
  245. codec->width = get_be16(pb);
  246. codec->height = get_be16(pb);
  247. codec->gop_size = get_be16(pb);
  248. codec->pix_fmt = get_be32(pb);
  249. codec->qmin = get_byte(pb);
  250. codec->qmax = get_byte(pb);
  251. codec->max_qdiff = get_byte(pb);
  252. codec->qcompress = get_be16(pb) / 10000.0;
  253. codec->qblur = get_be16(pb) / 10000.0;
  254. codec->bit_rate_tolerance = get_be32(pb);
  255. codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
  256. codec->rc_max_rate = get_be32(pb);
  257. codec->rc_min_rate = get_be32(pb);
  258. codec->rc_buffer_size = get_be32(pb);
  259. codec->i_quant_factor = av_int2dbl(get_be64(pb));
  260. codec->b_quant_factor = av_int2dbl(get_be64(pb));
  261. codec->i_quant_offset = av_int2dbl(get_be64(pb));
  262. codec->b_quant_offset = av_int2dbl(get_be64(pb));
  263. codec->dct_algo = get_be32(pb);
  264. codec->strict_std_compliance = get_be32(pb);
  265. codec->max_b_frames = get_be32(pb);
  266. codec->luma_elim_threshold = get_be32(pb);
  267. codec->chroma_elim_threshold = get_be32(pb);
  268. codec->mpeg_quant = get_be32(pb);
  269. codec->intra_dc_precision = get_be32(pb);
  270. codec->me_method = get_be32(pb);
  271. codec->mb_decision = get_be32(pb);
  272. codec->nsse_weight = get_be32(pb);
  273. codec->frame_skip_cmp = get_be32(pb);
  274. codec->rc_buffer_aggressivity = av_int2dbl(get_be64(pb));
  275. codec->codec_tag = get_be32(pb);
  276. break;
  277. case CODEC_TYPE_AUDIO:
  278. codec->sample_rate = get_be32(pb);
  279. codec->channels = get_le16(pb);
  280. codec->frame_size = get_le16(pb);
  281. break;
  282. default:
  283. goto fail;
  284. }
  285. }
  286. /* get until end of block reached */
  287. while ((url_ftell(pb) % ffm->packet_size) != 0)
  288. get_byte(pb);
  289. /* init packet demux */
  290. ffm->packet_ptr = ffm->packet;
  291. ffm->packet_end = ffm->packet;
  292. ffm->frame_offset = 0;
  293. ffm->pts = 0;
  294. ffm->read_state = READ_HEADER;
  295. ffm->first_packet = 1;
  296. return 0;
  297. fail:
  298. for(i=0;i<s->nb_streams;i++) {
  299. st = s->streams[i];
  300. if (st) {
  301. av_freep(&st->priv_data);
  302. av_free(st);
  303. }
  304. }
  305. return -1;
  306. }
  307. /* return < 0 if eof */
  308. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  309. {
  310. int size;
  311. FFMContext *ffm = s->priv_data;
  312. int duration;
  313. switch(ffm->read_state) {
  314. case READ_HEADER:
  315. if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
  316. return AVERROR(EAGAIN);
  317. }
  318. dprintf(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  319. url_ftell(s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
  320. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  321. FRAME_HEADER_SIZE)
  322. return AVERROR(EAGAIN);
  323. #if 0
  324. av_hexdump_log(s, AV_LOG_DEBUG, ffm->header, FRAME_HEADER_SIZE);
  325. #endif
  326. ffm->read_state = READ_DATA;
  327. /* fall thru */
  328. case READ_DATA:
  329. size = AV_RB24(ffm->header + 2);
  330. if (!ffm_is_avail_data(s, size)) {
  331. return AVERROR(EAGAIN);
  332. }
  333. duration = AV_RB24(ffm->header + 5);
  334. av_new_packet(pkt, size);
  335. pkt->stream_index = ffm->header[0];
  336. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  337. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  338. av_free_packet(pkt);
  339. ffm->read_state = READ_HEADER;
  340. return AVERROR(EAGAIN);
  341. }
  342. pkt->pos = url_ftell(s->pb);
  343. if (ffm->header[1] & FLAG_KEY_FRAME)
  344. pkt->flags |= PKT_FLAG_KEY;
  345. ffm->read_state = READ_HEADER;
  346. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  347. /* bad case: desynchronized packet. we cancel all the packet loading */
  348. av_free_packet(pkt);
  349. return AVERROR(EAGAIN);
  350. }
  351. if (ffm->first_frame_in_packet)
  352. {
  353. pkt->pts = ffm->pts;
  354. ffm->first_frame_in_packet = 0;
  355. }
  356. pkt->duration = duration;
  357. break;
  358. }
  359. return 0;
  360. }
  361. /* seek to a given time in the file. The file read pointer is
  362. positioned at or before pts. XXX: the following code is quite
  363. approximative */
  364. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  365. {
  366. FFMContext *ffm = s->priv_data;
  367. offset_t pos_min, pos_max, pos;
  368. int64_t pts_min, pts_max, pts;
  369. double pos1;
  370. #ifdef DEBUG_SEEK
  371. av_log(s, AV_LOG_DEBUG, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  372. #endif
  373. /* find the position using linear interpolation (better than
  374. dichotomy in typical cases) */
  375. pos_min = 0;
  376. pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
  377. while (pos_min <= pos_max) {
  378. pts_min = get_pts(s, pos_min);
  379. pts_max = get_pts(s, pos_max);
  380. /* linear interpolation */
  381. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  382. (double)(pts_max - pts_min);
  383. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  384. if (pos <= pos_min)
  385. pos = pos_min;
  386. else if (pos >= pos_max)
  387. pos = pos_max;
  388. pts = get_pts(s, pos);
  389. /* check if we are lucky */
  390. if (pts == wanted_pts) {
  391. goto found;
  392. } else if (pts > wanted_pts) {
  393. pos_max = pos - FFM_PACKET_SIZE;
  394. } else {
  395. pos_min = pos + FFM_PACKET_SIZE;
  396. }
  397. }
  398. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  399. if (pos > 0)
  400. pos -= FFM_PACKET_SIZE;
  401. found:
  402. ffm_seek1(s, pos);
  403. /* reset read state */
  404. ffm->read_state = READ_HEADER;
  405. ffm->packet_ptr = ffm->packet;
  406. ffm->packet_end = ffm->packet;
  407. ffm->first_packet = 1;
  408. return 0;
  409. }
  410. static int ffm_read_close(AVFormatContext *s)
  411. {
  412. AVStream *st;
  413. int i;
  414. for(i=0;i<s->nb_streams;i++) {
  415. st = s->streams[i];
  416. av_freep(&st->priv_data);
  417. }
  418. return 0;
  419. }
  420. static int ffm_probe(AVProbeData *p)
  421. {
  422. if (
  423. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  424. p->buf[3] == '1')
  425. return AVPROBE_SCORE_MAX + 1;
  426. return 0;
  427. }
  428. AVInputFormat ffm_demuxer = {
  429. "ffm",
  430. NULL_IF_CONFIG_SMALL("ffm format"),
  431. sizeof(FFMContext),
  432. ffm_probe,
  433. ffm_read_header,
  434. ffm_read_packet,
  435. ffm_read_close,
  436. ffm_seek,
  437. };