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.

498 lines
15KB

  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. if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
  286. codec->extradata_size = get_be32(pb);
  287. codec->extradata = av_malloc(codec->extradata_size);
  288. if (!codec->extradata)
  289. return AVERROR(ENOMEM);
  290. get_buffer(pb, codec->extradata, codec->extradata_size);
  291. }
  292. }
  293. /* get until end of block reached */
  294. while ((url_ftell(pb) % ffm->packet_size) != 0)
  295. get_byte(pb);
  296. /* init packet demux */
  297. ffm->packet_ptr = ffm->packet;
  298. ffm->packet_end = ffm->packet;
  299. ffm->frame_offset = 0;
  300. ffm->pts = 0;
  301. ffm->read_state = READ_HEADER;
  302. ffm->first_packet = 1;
  303. return 0;
  304. fail:
  305. for(i=0;i<s->nb_streams;i++) {
  306. st = s->streams[i];
  307. if (st) {
  308. av_freep(&st->priv_data);
  309. av_free(st);
  310. }
  311. }
  312. return -1;
  313. }
  314. /* return < 0 if eof */
  315. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  316. {
  317. int size;
  318. FFMContext *ffm = s->priv_data;
  319. int duration;
  320. switch(ffm->read_state) {
  321. case READ_HEADER:
  322. if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
  323. return AVERROR(EAGAIN);
  324. }
  325. dprintf(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  326. url_ftell(s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
  327. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  328. FRAME_HEADER_SIZE)
  329. return AVERROR(EAGAIN);
  330. #if 0
  331. av_hexdump_log(s, AV_LOG_DEBUG, ffm->header, FRAME_HEADER_SIZE);
  332. #endif
  333. ffm->read_state = READ_DATA;
  334. /* fall thru */
  335. case READ_DATA:
  336. size = AV_RB24(ffm->header + 2);
  337. if (!ffm_is_avail_data(s, size)) {
  338. return AVERROR(EAGAIN);
  339. }
  340. duration = AV_RB24(ffm->header + 5);
  341. av_new_packet(pkt, size);
  342. pkt->stream_index = ffm->header[0];
  343. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  344. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  345. av_free_packet(pkt);
  346. ffm->read_state = READ_HEADER;
  347. return AVERROR(EAGAIN);
  348. }
  349. pkt->pos = url_ftell(s->pb);
  350. if (ffm->header[1] & FLAG_KEY_FRAME)
  351. pkt->flags |= PKT_FLAG_KEY;
  352. ffm->read_state = READ_HEADER;
  353. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  354. /* bad case: desynchronized packet. we cancel all the packet loading */
  355. av_free_packet(pkt);
  356. return AVERROR(EAGAIN);
  357. }
  358. if (ffm->first_frame_in_packet)
  359. {
  360. pkt->pts = ffm->pts;
  361. ffm->first_frame_in_packet = 0;
  362. }
  363. pkt->duration = duration;
  364. break;
  365. }
  366. return 0;
  367. }
  368. /* seek to a given time in the file. The file read pointer is
  369. positioned at or before pts. XXX: the following code is quite
  370. approximative */
  371. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  372. {
  373. FFMContext *ffm = s->priv_data;
  374. offset_t pos_min, pos_max, pos;
  375. int64_t pts_min, pts_max, pts;
  376. double pos1;
  377. #ifdef DEBUG_SEEK
  378. av_log(s, AV_LOG_DEBUG, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  379. #endif
  380. /* find the position using linear interpolation (better than
  381. dichotomy in typical cases) */
  382. pos_min = 0;
  383. pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
  384. while (pos_min <= pos_max) {
  385. pts_min = get_pts(s, pos_min);
  386. pts_max = get_pts(s, pos_max);
  387. /* linear interpolation */
  388. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  389. (double)(pts_max - pts_min);
  390. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  391. if (pos <= pos_min)
  392. pos = pos_min;
  393. else if (pos >= pos_max)
  394. pos = pos_max;
  395. pts = get_pts(s, pos);
  396. /* check if we are lucky */
  397. if (pts == wanted_pts) {
  398. goto found;
  399. } else if (pts > wanted_pts) {
  400. pos_max = pos - FFM_PACKET_SIZE;
  401. } else {
  402. pos_min = pos + FFM_PACKET_SIZE;
  403. }
  404. }
  405. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  406. if (pos > 0)
  407. pos -= FFM_PACKET_SIZE;
  408. found:
  409. ffm_seek1(s, pos);
  410. /* reset read state */
  411. ffm->read_state = READ_HEADER;
  412. ffm->packet_ptr = ffm->packet;
  413. ffm->packet_end = ffm->packet;
  414. ffm->first_packet = 1;
  415. return 0;
  416. }
  417. static int ffm_read_close(AVFormatContext *s)
  418. {
  419. AVStream *st;
  420. int i;
  421. for(i=0;i<s->nb_streams;i++) {
  422. st = s->streams[i];
  423. av_freep(&st->priv_data);
  424. }
  425. return 0;
  426. }
  427. static int ffm_probe(AVProbeData *p)
  428. {
  429. if (
  430. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  431. p->buf[3] == '1')
  432. return AVPROBE_SCORE_MAX + 1;
  433. return 0;
  434. }
  435. AVInputFormat ffm_demuxer = {
  436. "ffm",
  437. NULL_IF_CONFIG_SMALL("ffm format"),
  438. sizeof(FFMContext),
  439. ffm_probe,
  440. ffm_read_header,
  441. ffm_read_packet,
  442. ffm_read_close,
  443. ffm_seek,
  444. };