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.

488 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. int64_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, int64_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, int64_t pos, int64_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. int64_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->dts = get_be64(pb);
  93. frame_offset = get_be16(pb);
  94. get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
  95. ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
  96. if (ffm->packet_end < ffm->packet || frame_offset < 0)
  97. return -1;
  98. /* if first packet or resynchronization packet, we must
  99. handle it specifically */
  100. if (ffm->first_packet || (frame_offset & 0x8000)) {
  101. if (!frame_offset) {
  102. /* This packet has no frame headers in it */
  103. if (url_ftell(pb) >= ffm->packet_size * 3) {
  104. url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
  105. goto retry_read;
  106. }
  107. /* This is bad, we cannot find a valid frame header */
  108. return 0;
  109. }
  110. ffm->first_packet = 0;
  111. if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
  112. return -1;
  113. ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
  114. if (!header)
  115. break;
  116. } else {
  117. ffm->packet_ptr = ffm->packet;
  118. }
  119. goto redo;
  120. }
  121. memcpy(buf, ffm->packet_ptr, len);
  122. buf += len;
  123. ffm->packet_ptr += len;
  124. size -= len;
  125. header = 0;
  126. }
  127. return size1 - size;
  128. }
  129. //#define DEBUG_SEEK
  130. /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
  131. by the write position inside this function */
  132. static void ffm_seek1(AVFormatContext *s, int64_t pos1)
  133. {
  134. FFMContext *ffm = s->priv_data;
  135. ByteIOContext *pb = s->pb;
  136. int64_t pos;
  137. pos = pos1 + ffm->write_index;
  138. if (pos >= ffm->file_size)
  139. pos -= (ffm->file_size - FFM_PACKET_SIZE);
  140. #ifdef DEBUG_SEEK
  141. av_log(s, AV_LOG_DEBUG, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
  142. #endif
  143. url_fseek(pb, pos, SEEK_SET);
  144. }
  145. static int64_t get_dts(AVFormatContext *s, int64_t pos)
  146. {
  147. ByteIOContext *pb = s->pb;
  148. int64_t dts;
  149. ffm_seek1(s, pos);
  150. url_fskip(pb, 4);
  151. dts = get_be64(pb);
  152. #ifdef DEBUG_SEEK
  153. av_log(s, AV_LOG_DEBUG, "pts=%0.6f\n", pts / 1000000.0);
  154. #endif
  155. return dts;
  156. }
  157. static void adjust_write_index(AVFormatContext *s)
  158. {
  159. FFMContext *ffm = s->priv_data;
  160. ByteIOContext *pb = s->pb;
  161. int64_t pts;
  162. //int64_t orig_write_index = ffm->write_index;
  163. int64_t pos_min, pos_max;
  164. int64_t pts_start;
  165. int64_t ptr = url_ftell(pb);
  166. pos_min = 0;
  167. pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
  168. pts_start = get_dts(s, pos_min);
  169. pts = get_dts(s, pos_max);
  170. if (pts - 100000 > pts_start)
  171. goto end;
  172. ffm->write_index = FFM_PACKET_SIZE;
  173. pts_start = get_dts(s, pos_min);
  174. pts = get_dts(s, pos_max);
  175. if (pts - 100000 <= pts_start) {
  176. while (1) {
  177. int64_t newpos;
  178. int64_t newpts;
  179. newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
  180. if (newpos == pos_min)
  181. break;
  182. newpts = get_dts(s, newpos);
  183. if (newpts - 100000 <= pts) {
  184. pos_max = newpos;
  185. pts = newpts;
  186. } else {
  187. pos_min = newpos;
  188. }
  189. }
  190. ffm->write_index += pos_max;
  191. }
  192. //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
  193. //printf("pts range %0.6f - %0.6f\n", get_dts(s, 0) / 1000000. , get_dts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
  194. end:
  195. url_fseek(pb, ptr, SEEK_SET);
  196. }
  197. static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
  198. {
  199. FFMContext *ffm = s->priv_data;
  200. AVStream *st;
  201. ByteIOContext *pb = s->pb;
  202. AVCodecContext *codec;
  203. int i, nb_streams;
  204. uint32_t tag;
  205. /* header */
  206. tag = get_le32(pb);
  207. if (tag != MKTAG('F', 'F', 'M', '1'))
  208. goto fail;
  209. ffm->packet_size = get_be32(pb);
  210. if (ffm->packet_size != FFM_PACKET_SIZE)
  211. goto fail;
  212. ffm->write_index = get_be64(pb);
  213. /* get also filesize */
  214. if (!url_is_streamed(pb)) {
  215. ffm->file_size = url_fsize(pb);
  216. adjust_write_index(s);
  217. } else {
  218. ffm->file_size = (UINT64_C(1) << 63) - 1;
  219. }
  220. nb_streams = get_be32(pb);
  221. get_be32(pb); /* total bitrate */
  222. /* read each stream */
  223. for(i=0;i<nb_streams;i++) {
  224. char rc_eq_buf[128];
  225. st = av_new_stream(s, 0);
  226. if (!st)
  227. goto fail;
  228. s->streams[i] = st;
  229. av_set_pts_info(st, 64, 1, 1000000);
  230. codec = st->codec;
  231. /* generic info */
  232. codec->codec_id = get_be32(pb);
  233. codec->codec_type = get_byte(pb); /* codec_type */
  234. codec->bit_rate = get_be32(pb);
  235. st->quality = get_be32(pb);
  236. codec->flags = get_be32(pb);
  237. codec->flags2 = get_be32(pb);
  238. codec->debug = get_be32(pb);
  239. /* specific info */
  240. switch(codec->codec_type) {
  241. case CODEC_TYPE_VIDEO:
  242. codec->time_base.num = get_be32(pb);
  243. codec->time_base.den = get_be32(pb);
  244. codec->width = get_be16(pb);
  245. codec->height = get_be16(pb);
  246. codec->gop_size = get_be16(pb);
  247. codec->pix_fmt = get_be32(pb);
  248. codec->qmin = get_byte(pb);
  249. codec->qmax = get_byte(pb);
  250. codec->max_qdiff = get_byte(pb);
  251. codec->qcompress = get_be16(pb) / 10000.0;
  252. codec->qblur = get_be16(pb) / 10000.0;
  253. codec->bit_rate_tolerance = get_be32(pb);
  254. codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
  255. codec->rc_max_rate = get_be32(pb);
  256. codec->rc_min_rate = get_be32(pb);
  257. codec->rc_buffer_size = get_be32(pb);
  258. codec->i_quant_factor = av_int2dbl(get_be64(pb));
  259. codec->b_quant_factor = av_int2dbl(get_be64(pb));
  260. codec->i_quant_offset = av_int2dbl(get_be64(pb));
  261. codec->b_quant_offset = av_int2dbl(get_be64(pb));
  262. codec->dct_algo = get_be32(pb);
  263. codec->strict_std_compliance = get_be32(pb);
  264. codec->max_b_frames = get_be32(pb);
  265. codec->luma_elim_threshold = get_be32(pb);
  266. codec->chroma_elim_threshold = get_be32(pb);
  267. codec->mpeg_quant = get_be32(pb);
  268. codec->intra_dc_precision = get_be32(pb);
  269. codec->me_method = get_be32(pb);
  270. codec->mb_decision = get_be32(pb);
  271. codec->nsse_weight = get_be32(pb);
  272. codec->frame_skip_cmp = get_be32(pb);
  273. codec->rc_buffer_aggressivity = av_int2dbl(get_be64(pb));
  274. codec->codec_tag = get_be32(pb);
  275. codec->thread_count = get_byte(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->dts = 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_free(st);
  309. }
  310. }
  311. return -1;
  312. }
  313. /* return < 0 if eof */
  314. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  315. {
  316. int size;
  317. FFMContext *ffm = s->priv_data;
  318. int duration;
  319. switch(ffm->read_state) {
  320. case READ_HEADER:
  321. if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) {
  322. return AVERROR(EAGAIN);
  323. }
  324. dprintf(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  325. url_ftell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
  326. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  327. FRAME_HEADER_SIZE)
  328. return AVERROR(EAGAIN);
  329. if (ffm->header[1] & FLAG_DTS)
  330. if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
  331. return AVERROR(EAGAIN);
  332. #if 0
  333. av_hexdump_log(s, AV_LOG_DEBUG, ffm->header, FRAME_HEADER_SIZE);
  334. #endif
  335. ffm->read_state = READ_DATA;
  336. /* fall thru */
  337. case READ_DATA:
  338. size = AV_RB24(ffm->header + 2);
  339. if (!ffm_is_avail_data(s, size)) {
  340. return AVERROR(EAGAIN);
  341. }
  342. duration = AV_RB24(ffm->header + 5);
  343. av_new_packet(pkt, size);
  344. pkt->stream_index = ffm->header[0];
  345. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  346. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  347. av_free_packet(pkt);
  348. ffm->read_state = READ_HEADER;
  349. return AVERROR(EAGAIN);
  350. }
  351. pkt->pos = url_ftell(s->pb);
  352. if (ffm->header[1] & FLAG_KEY_FRAME)
  353. pkt->flags |= PKT_FLAG_KEY;
  354. ffm->read_state = READ_HEADER;
  355. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  356. /* bad case: desynchronized packet. we cancel all the packet loading */
  357. av_free_packet(pkt);
  358. return AVERROR(EAGAIN);
  359. }
  360. pkt->pts = AV_RB64(ffm->header+8);
  361. if (ffm->header[1] & FLAG_DTS)
  362. pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
  363. else
  364. pkt->dts = pkt->pts;
  365. pkt->duration = duration;
  366. break;
  367. }
  368. return 0;
  369. }
  370. /* seek to a given time in the file. The file read pointer is
  371. positioned at or before pts. XXX: the following code is quite
  372. approximative */
  373. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  374. {
  375. FFMContext *ffm = s->priv_data;
  376. int64_t pos_min, pos_max, pos;
  377. int64_t pts_min, pts_max, pts;
  378. double pos1;
  379. #ifdef DEBUG_SEEK
  380. av_log(s, AV_LOG_DEBUG, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  381. #endif
  382. /* find the position using linear interpolation (better than
  383. dichotomy in typical cases) */
  384. pos_min = 0;
  385. pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
  386. while (pos_min <= pos_max) {
  387. pts_min = get_dts(s, pos_min);
  388. pts_max = get_dts(s, pos_max);
  389. /* linear interpolation */
  390. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  391. (double)(pts_max - pts_min);
  392. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  393. if (pos <= pos_min)
  394. pos = pos_min;
  395. else if (pos >= pos_max)
  396. pos = pos_max;
  397. pts = get_dts(s, pos);
  398. /* check if we are lucky */
  399. if (pts == wanted_pts) {
  400. goto found;
  401. } else if (pts > wanted_pts) {
  402. pos_max = pos - FFM_PACKET_SIZE;
  403. } else {
  404. pos_min = pos + FFM_PACKET_SIZE;
  405. }
  406. }
  407. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  408. if (pos > 0)
  409. pos -= FFM_PACKET_SIZE;
  410. found:
  411. ffm_seek1(s, pos);
  412. /* reset read state */
  413. ffm->read_state = READ_HEADER;
  414. ffm->packet_ptr = ffm->packet;
  415. ffm->packet_end = ffm->packet;
  416. ffm->first_packet = 1;
  417. return 0;
  418. }
  419. static int ffm_probe(AVProbeData *p)
  420. {
  421. if (
  422. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  423. p->buf[3] == '1')
  424. return AVPROBE_SCORE_MAX + 1;
  425. return 0;
  426. }
  427. AVInputFormat ffm_demuxer = {
  428. "ffm",
  429. NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
  430. sizeof(FFMContext),
  431. ffm_probe,
  432. ffm_read_header,
  433. ffm_read_packet,
  434. NULL,
  435. ffm_seek,
  436. };