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
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 "libavutil/intreadwrite.h"
  22. #include "avformat.h"
  23. #include "ffm.h"
  24. #if CONFIG_FFSERVER
  25. #include <unistd.h>
  26. int64_t ffm_read_write_index(int fd)
  27. {
  28. uint8_t buf[8];
  29. lseek(fd, 8, SEEK_SET);
  30. read(fd, buf, 8);
  31. return AV_RB64(buf);
  32. }
  33. void ffm_write_write_index(int fd, int64_t pos)
  34. {
  35. uint8_t buf[8];
  36. int i;
  37. for(i=0;i<8;i++)
  38. buf[i] = (pos >> (56 - i * 8)) & 0xff;
  39. lseek(fd, 8, SEEK_SET);
  40. write(fd, buf, 8);
  41. }
  42. void ffm_set_write_index(AVFormatContext *s, int64_t pos, int64_t file_size)
  43. {
  44. FFMContext *ffm = s->priv_data;
  45. ffm->write_index = pos;
  46. ffm->file_size = file_size;
  47. }
  48. #endif // CONFIG_FFSERVER
  49. static int ffm_is_avail_data(AVFormatContext *s, int size)
  50. {
  51. FFMContext *ffm = s->priv_data;
  52. int64_t pos, avail_size;
  53. int len;
  54. len = ffm->packet_end - ffm->packet_ptr;
  55. if (size <= len)
  56. return 1;
  57. pos = url_ftell(s->pb);
  58. if (pos == ffm->write_index) {
  59. /* exactly at the end of stream */
  60. return 0;
  61. } else if (pos < ffm->write_index) {
  62. avail_size = ffm->write_index - pos;
  63. } else {
  64. avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
  65. }
  66. avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
  67. if (size <= avail_size)
  68. return 1;
  69. else
  70. return 0;
  71. }
  72. /* first is true if we read the frame header */
  73. static int ffm_read_data(AVFormatContext *s,
  74. uint8_t *buf, int size, int header)
  75. {
  76. FFMContext *ffm = s->priv_data;
  77. ByteIOContext *pb = s->pb;
  78. int len, fill_size, size1, frame_offset;
  79. size1 = size;
  80. while (size > 0) {
  81. redo:
  82. len = ffm->packet_end - ffm->packet_ptr;
  83. if (len < 0)
  84. return -1;
  85. if (len > size)
  86. len = size;
  87. if (len == 0) {
  88. if (url_ftell(pb) == ffm->file_size)
  89. url_fseek(pb, ffm->packet_size, SEEK_SET);
  90. retry_read:
  91. get_be16(pb); /* PACKET_ID */
  92. fill_size = get_be16(pb);
  93. ffm->dts = get_be64(pb);
  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, int64_t pos1)
  134. {
  135. FFMContext *ffm = s->priv_data;
  136. ByteIOContext *pb = s->pb;
  137. int64_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_dts(AVFormatContext *s, int64_t pos)
  147. {
  148. ByteIOContext *pb = s->pb;
  149. int64_t dts;
  150. ffm_seek1(s, pos);
  151. url_fskip(pb, 4);
  152. dts = 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 dts;
  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. //int64_t orig_write_index = ffm->write_index;
  164. int64_t pos_min, pos_max;
  165. int64_t pts_start;
  166. int64_t ptr = url_ftell(pb);
  167. pos_min = 0;
  168. pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
  169. pts_start = get_dts(s, pos_min);
  170. pts = get_dts(s, pos_max);
  171. if (pts - 100000 > pts_start)
  172. goto end;
  173. ffm->write_index = FFM_PACKET_SIZE;
  174. pts_start = get_dts(s, pos_min);
  175. pts = get_dts(s, pos_max);
  176. if (pts - 100000 <= pts_start) {
  177. while (1) {
  178. int64_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_dts(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_dts(s, 0) / 1000000. , get_dts(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. codec->thread_count = get_byte(pb);
  277. break;
  278. case CODEC_TYPE_AUDIO:
  279. codec->sample_rate = get_be32(pb);
  280. codec->channels = get_le16(pb);
  281. codec->frame_size = get_le16(pb);
  282. break;
  283. default:
  284. goto fail;
  285. }
  286. if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
  287. codec->extradata_size = get_be32(pb);
  288. codec->extradata = av_malloc(codec->extradata_size);
  289. if (!codec->extradata)
  290. return AVERROR(ENOMEM);
  291. get_buffer(pb, codec->extradata, codec->extradata_size);
  292. }
  293. }
  294. /* get until end of block reached */
  295. while ((url_ftell(pb) % ffm->packet_size) != 0)
  296. get_byte(pb);
  297. /* init packet demux */
  298. ffm->packet_ptr = ffm->packet;
  299. ffm->packet_end = ffm->packet;
  300. ffm->frame_offset = 0;
  301. ffm->dts = 0;
  302. ffm->read_state = READ_HEADER;
  303. ffm->first_packet = 1;
  304. return 0;
  305. fail:
  306. for(i=0;i<s->nb_streams;i++) {
  307. st = s->streams[i];
  308. if (st) {
  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. if (url_fsize(s->pb) == FFM_PACKET_SIZE)
  321. return -1;
  322. switch(ffm->read_state) {
  323. case READ_HEADER:
  324. if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) {
  325. return AVERROR(EAGAIN);
  326. }
  327. dprintf(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  328. url_ftell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
  329. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  330. FRAME_HEADER_SIZE)
  331. return AVERROR(EAGAIN);
  332. if (ffm->header[1] & FLAG_DTS)
  333. if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
  334. return AVERROR(EAGAIN);
  335. #if 0
  336. av_hexdump_log(s, AV_LOG_DEBUG, ffm->header, FRAME_HEADER_SIZE);
  337. #endif
  338. ffm->read_state = READ_DATA;
  339. /* fall thru */
  340. case READ_DATA:
  341. size = AV_RB24(ffm->header + 2);
  342. if (!ffm_is_avail_data(s, size)) {
  343. return AVERROR(EAGAIN);
  344. }
  345. duration = AV_RB24(ffm->header + 5);
  346. av_new_packet(pkt, size);
  347. pkt->stream_index = ffm->header[0];
  348. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  349. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  350. av_free_packet(pkt);
  351. ffm->read_state = READ_HEADER;
  352. return AVERROR(EAGAIN);
  353. }
  354. pkt->pos = url_ftell(s->pb);
  355. if (ffm->header[1] & FLAG_KEY_FRAME)
  356. pkt->flags |= PKT_FLAG_KEY;
  357. ffm->read_state = READ_HEADER;
  358. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  359. /* bad case: desynchronized packet. we cancel all the packet loading */
  360. av_free_packet(pkt);
  361. return AVERROR(EAGAIN);
  362. }
  363. pkt->pts = AV_RB64(ffm->header+8);
  364. if (ffm->header[1] & FLAG_DTS)
  365. pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
  366. else
  367. pkt->dts = pkt->pts;
  368. pkt->duration = duration;
  369. break;
  370. }
  371. return 0;
  372. }
  373. /* seek to a given time in the file. The file read pointer is
  374. positioned at or before pts. XXX: the following code is quite
  375. approximative */
  376. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  377. {
  378. FFMContext *ffm = s->priv_data;
  379. int64_t pos_min, pos_max, pos;
  380. int64_t pts_min, pts_max, pts;
  381. double pos1;
  382. #ifdef DEBUG_SEEK
  383. av_log(s, AV_LOG_DEBUG, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  384. #endif
  385. /* find the position using linear interpolation (better than
  386. dichotomy in typical cases) */
  387. pos_min = 0;
  388. pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
  389. while (pos_min <= pos_max) {
  390. pts_min = get_dts(s, pos_min);
  391. pts_max = get_dts(s, pos_max);
  392. /* linear interpolation */
  393. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  394. (double)(pts_max - pts_min);
  395. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  396. if (pos <= pos_min)
  397. pos = pos_min;
  398. else if (pos >= pos_max)
  399. pos = pos_max;
  400. pts = get_dts(s, pos);
  401. /* check if we are lucky */
  402. if (pts == wanted_pts) {
  403. goto found;
  404. } else if (pts > wanted_pts) {
  405. pos_max = pos - FFM_PACKET_SIZE;
  406. } else {
  407. pos_min = pos + FFM_PACKET_SIZE;
  408. }
  409. }
  410. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  411. if (pos > 0)
  412. pos -= FFM_PACKET_SIZE;
  413. found:
  414. ffm_seek1(s, pos);
  415. /* reset read state */
  416. ffm->read_state = READ_HEADER;
  417. ffm->packet_ptr = ffm->packet;
  418. ffm->packet_end = ffm->packet;
  419. ffm->first_packet = 1;
  420. return 0;
  421. }
  422. static int ffm_probe(AVProbeData *p)
  423. {
  424. if (
  425. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  426. p->buf[3] == '1')
  427. return AVPROBE_SCORE_MAX + 1;
  428. return 0;
  429. }
  430. AVInputFormat ffm_demuxer = {
  431. "ffm",
  432. NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
  433. sizeof(FFMContext),
  434. ffm_probe,
  435. ffm_read_header,
  436. ffm_read_packet,
  437. NULL,
  438. ffm_seek,
  439. };