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.

766 lines
25KB

  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 <stdint.h>
  22. #include "libavutil/internal.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/intfloat.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "avformat.h"
  29. #include "internal.h"
  30. #include "ffm.h"
  31. #include "avio_internal.h"
  32. static int ffm_is_avail_data(AVFormatContext *s, int size)
  33. {
  34. FFMContext *ffm = s->priv_data;
  35. int64_t pos, avail_size;
  36. int len;
  37. len = ffm->packet_end - ffm->packet_ptr;
  38. if (size <= len)
  39. return 1;
  40. pos = avio_tell(s->pb);
  41. if (!ffm->write_index) {
  42. if (pos == ffm->file_size)
  43. return AVERROR_EOF;
  44. avail_size = ffm->file_size - pos;
  45. } else {
  46. if (pos == ffm->write_index) {
  47. /* exactly at the end of stream */
  48. if (ffm->server_attached)
  49. return AVERROR(EAGAIN);
  50. else
  51. return AVERROR_INVALIDDATA;
  52. } else if (pos < ffm->write_index) {
  53. avail_size = ffm->write_index - pos;
  54. } else {
  55. avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
  56. }
  57. }
  58. avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
  59. if (size <= avail_size)
  60. return 1;
  61. else if (ffm->server_attached)
  62. return AVERROR(EAGAIN);
  63. else
  64. return AVERROR_INVALIDDATA;
  65. }
  66. static int ffm_resync(AVFormatContext *s, int state)
  67. {
  68. av_log(s, AV_LOG_ERROR, "resyncing\n");
  69. while (state != PACKET_ID) {
  70. if (avio_feof(s->pb)) {
  71. av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
  72. return -1;
  73. }
  74. state = (state << 8) | avio_r8(s->pb);
  75. }
  76. return 0;
  77. }
  78. /* first is true if we read the frame header */
  79. static int ffm_read_data(AVFormatContext *s,
  80. uint8_t *buf, int size, int header)
  81. {
  82. FFMContext *ffm = s->priv_data;
  83. AVIOContext *pb = s->pb;
  84. int len, fill_size, size1, frame_offset, id;
  85. int64_t last_pos = -1;
  86. size1 = size;
  87. while (size > 0) {
  88. redo:
  89. len = ffm->packet_end - ffm->packet_ptr;
  90. if (len < 0)
  91. return -1;
  92. if (len > size)
  93. len = size;
  94. if (len == 0) {
  95. if (avio_tell(pb) == ffm->file_size)
  96. if (ffm->server_attached)
  97. avio_seek(pb, ffm->packet_size, SEEK_SET);
  98. else
  99. return AVERROR_EOF;
  100. retry_read:
  101. if (pb->buffer_size != ffm->packet_size) {
  102. int64_t tell = avio_tell(pb);
  103. int ret = ffio_set_buf_size(pb, ffm->packet_size);
  104. if (ret < 0)
  105. return ret;
  106. avio_seek(pb, tell, SEEK_SET);
  107. }
  108. id = avio_rb16(pb); /* PACKET_ID */
  109. if (id != PACKET_ID) {
  110. if (ffm_resync(s, id) < 0)
  111. return -1;
  112. last_pos = avio_tell(pb);
  113. }
  114. fill_size = avio_rb16(pb);
  115. ffm->dts = avio_rb64(pb);
  116. frame_offset = avio_rb16(pb);
  117. avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
  118. ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
  119. if (ffm->packet_end < ffm->packet || frame_offset < 0)
  120. return -1;
  121. /* if first packet or resynchronization packet, we must
  122. handle it specifically */
  123. if (ffm->first_packet || (frame_offset & 0x8000)) {
  124. if (!frame_offset) {
  125. /* This packet has no frame headers in it */
  126. if (avio_tell(pb) >= ffm->packet_size * 3LL) {
  127. int64_t seekback = FFMIN(ffm->packet_size * 2LL, avio_tell(pb) - last_pos);
  128. seekback = FFMAX(seekback, 0);
  129. avio_seek(pb, -seekback, SEEK_CUR);
  130. goto retry_read;
  131. }
  132. /* This is bad, we cannot find a valid frame header */
  133. return 0;
  134. }
  135. ffm->first_packet = 0;
  136. if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
  137. return -1;
  138. ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
  139. if (!header)
  140. break;
  141. } else {
  142. ffm->packet_ptr = ffm->packet;
  143. }
  144. goto redo;
  145. }
  146. memcpy(buf, ffm->packet_ptr, len);
  147. buf += len;
  148. ffm->packet_ptr += len;
  149. size -= len;
  150. header = 0;
  151. }
  152. return size1 - size;
  153. }
  154. /* ensure that actual seeking happens between FFM_PACKET_SIZE
  155. and file_size - FFM_PACKET_SIZE */
  156. static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
  157. {
  158. FFMContext *ffm = s->priv_data;
  159. AVIOContext *pb = s->pb;
  160. int64_t pos;
  161. pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
  162. pos = FFMAX(pos, FFM_PACKET_SIZE);
  163. ff_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
  164. return avio_seek(pb, pos, SEEK_SET);
  165. }
  166. static int64_t get_dts(AVFormatContext *s, int64_t pos)
  167. {
  168. AVIOContext *pb = s->pb;
  169. int64_t dts;
  170. ffm_seek1(s, pos);
  171. avio_skip(pb, 4);
  172. dts = avio_rb64(pb);
  173. ff_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
  174. return dts;
  175. }
  176. static void adjust_write_index(AVFormatContext *s)
  177. {
  178. FFMContext *ffm = s->priv_data;
  179. AVIOContext *pb = s->pb;
  180. int64_t pts;
  181. //int64_t orig_write_index = ffm->write_index;
  182. int64_t pos_min, pos_max;
  183. int64_t pts_start;
  184. int64_t ptr = avio_tell(pb);
  185. pos_min = 0;
  186. pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
  187. pts_start = get_dts(s, pos_min);
  188. pts = get_dts(s, pos_max);
  189. if (pts - 100000 > pts_start)
  190. goto end;
  191. ffm->write_index = FFM_PACKET_SIZE;
  192. pts_start = get_dts(s, pos_min);
  193. pts = get_dts(s, pos_max);
  194. if (pts - 100000 <= pts_start) {
  195. while (1) {
  196. int64_t newpos;
  197. int64_t newpts;
  198. newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
  199. if (newpos == pos_min)
  200. break;
  201. newpts = get_dts(s, newpos);
  202. if (newpts - 100000 <= pts) {
  203. pos_max = newpos;
  204. pts = newpts;
  205. } else {
  206. pos_min = newpos;
  207. }
  208. }
  209. ffm->write_index += pos_max;
  210. }
  211. end:
  212. avio_seek(pb, ptr, SEEK_SET);
  213. }
  214. static int ffm_close(AVFormatContext *s)
  215. {
  216. int i;
  217. for (i = 0; i < s->nb_streams; i++)
  218. av_freep(&s->streams[i]->codec->rc_eq);
  219. return 0;
  220. }
  221. static int ffm_append_recommended_configuration(AVStream *st, char **conf)
  222. {
  223. int ret;
  224. size_t newsize;
  225. av_assert0(conf && st);
  226. if (!*conf)
  227. return 0;
  228. if (!st->recommended_encoder_configuration) {
  229. st->recommended_encoder_configuration = *conf;
  230. *conf = 0;
  231. return 0;
  232. }
  233. newsize = strlen(*conf) + strlen(st->recommended_encoder_configuration) + 2;
  234. if ((ret = av_reallocp(&st->recommended_encoder_configuration, newsize)) < 0)
  235. return ret;
  236. av_strlcat(st->recommended_encoder_configuration, ",", newsize);
  237. av_strlcat(st->recommended_encoder_configuration, *conf, newsize);
  238. av_freep(conf);
  239. return 0;
  240. }
  241. static int ffm2_read_header(AVFormatContext *s)
  242. {
  243. FFMContext *ffm = s->priv_data;
  244. AVStream *st;
  245. AVIOContext *pb = s->pb;
  246. AVCodecContext *codec;
  247. int ret;
  248. int f_main = 0, f_cprv = -1, f_stvi = -1, f_stau = -1;
  249. AVCodec *enc;
  250. char *buffer;
  251. ffm->packet_size = avio_rb32(pb);
  252. if (ffm->packet_size != FFM_PACKET_SIZE) {
  253. av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
  254. ffm->packet_size, FFM_PACKET_SIZE);
  255. ret = AVERROR_INVALIDDATA;
  256. goto fail;
  257. }
  258. ffm->write_index = avio_rb64(pb);
  259. /* get also filesize */
  260. if (pb->seekable) {
  261. ffm->file_size = avio_size(pb);
  262. if (ffm->write_index && 0)
  263. adjust_write_index(s);
  264. } else {
  265. ffm->file_size = (UINT64_C(1) << 63) - 1;
  266. }
  267. while(!avio_feof(pb)) {
  268. unsigned id = avio_rb32(pb);
  269. unsigned size = avio_rb32(pb);
  270. int64_t next = avio_tell(pb) + size;
  271. char rc_eq_buf[128];
  272. if(!id)
  273. break;
  274. switch(id) {
  275. case MKBETAG('M', 'A', 'I', 'N'):
  276. if (f_main++) {
  277. ret = AVERROR(EINVAL);
  278. goto fail;
  279. }
  280. avio_rb32(pb); /* nb_streams */
  281. avio_rb32(pb); /* total bitrate */
  282. break;
  283. case MKBETAG('C', 'O', 'M', 'M'):
  284. f_cprv = f_stvi = f_stau = 0;
  285. st = avformat_new_stream(s, NULL);
  286. if (!st) {
  287. ret = AVERROR(ENOMEM);
  288. goto fail;
  289. }
  290. avpriv_set_pts_info(st, 64, 1, 1000000);
  291. codec = st->codec;
  292. /* generic info */
  293. codec->codec_id = avio_rb32(pb);
  294. codec->codec_type = avio_r8(pb);
  295. codec->bit_rate = avio_rb32(pb);
  296. codec->flags = avio_rb32(pb);
  297. codec->flags2 = avio_rb32(pb);
  298. codec->debug = avio_rb32(pb);
  299. if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  300. if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
  301. return AVERROR(ENOMEM);
  302. }
  303. break;
  304. case MKBETAG('S', 'T', 'V', 'I'):
  305. if (f_stvi++) {
  306. ret = AVERROR(EINVAL);
  307. goto fail;
  308. }
  309. codec->time_base.num = avio_rb32(pb);
  310. codec->time_base.den = avio_rb32(pb);
  311. if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
  312. av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
  313. codec->time_base.num, codec->time_base.den);
  314. ret = AVERROR_INVALIDDATA;
  315. goto fail;
  316. }
  317. codec->width = avio_rb16(pb);
  318. codec->height = avio_rb16(pb);
  319. codec->gop_size = avio_rb16(pb);
  320. codec->pix_fmt = avio_rb32(pb);
  321. codec->qmin = avio_r8(pb);
  322. codec->qmax = avio_r8(pb);
  323. codec->max_qdiff = avio_r8(pb);
  324. codec->qcompress = avio_rb16(pb) / 10000.0;
  325. codec->qblur = avio_rb16(pb) / 10000.0;
  326. codec->bit_rate_tolerance = avio_rb32(pb);
  327. avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  328. codec->rc_eq = av_strdup(rc_eq_buf);
  329. codec->rc_max_rate = avio_rb32(pb);
  330. codec->rc_min_rate = avio_rb32(pb);
  331. codec->rc_buffer_size = avio_rb32(pb);
  332. codec->i_quant_factor = av_int2double(avio_rb64(pb));
  333. codec->b_quant_factor = av_int2double(avio_rb64(pb));
  334. codec->i_quant_offset = av_int2double(avio_rb64(pb));
  335. codec->b_quant_offset = av_int2double(avio_rb64(pb));
  336. codec->dct_algo = avio_rb32(pb);
  337. codec->strict_std_compliance = avio_rb32(pb);
  338. codec->max_b_frames = avio_rb32(pb);
  339. codec->mpeg_quant = avio_rb32(pb);
  340. codec->intra_dc_precision = avio_rb32(pb);
  341. codec->me_method = avio_rb32(pb);
  342. codec->mb_decision = avio_rb32(pb);
  343. codec->nsse_weight = avio_rb32(pb);
  344. codec->frame_skip_cmp = avio_rb32(pb);
  345. codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
  346. codec->codec_tag = avio_rb32(pb);
  347. codec->thread_count = avio_r8(pb);
  348. codec->coder_type = avio_rb32(pb);
  349. codec->me_cmp = avio_rb32(pb);
  350. codec->me_subpel_quality = avio_rb32(pb);
  351. codec->me_range = avio_rb32(pb);
  352. codec->keyint_min = avio_rb32(pb);
  353. codec->scenechange_threshold = avio_rb32(pb);
  354. codec->b_frame_strategy = avio_rb32(pb);
  355. codec->qcompress = av_int2double(avio_rb64(pb));
  356. codec->qblur = av_int2double(avio_rb64(pb));
  357. codec->max_qdiff = avio_rb32(pb);
  358. codec->refs = avio_rb32(pb);
  359. break;
  360. case MKBETAG('S', 'T', 'A', 'U'):
  361. if (f_stau++) {
  362. ret = AVERROR(EINVAL);
  363. goto fail;
  364. }
  365. codec->sample_rate = avio_rb32(pb);
  366. codec->channels = avio_rl16(pb);
  367. codec->frame_size = avio_rl16(pb);
  368. break;
  369. case MKBETAG('C', 'P', 'R', 'V'):
  370. if (f_cprv++) {
  371. ret = AVERROR(EINVAL);
  372. goto fail;
  373. }
  374. enc = avcodec_find_encoder(codec->codec_id);
  375. if (enc && enc->priv_data_size && enc->priv_class) {
  376. buffer = av_malloc(size + 1);
  377. if (!buffer) {
  378. ret = AVERROR(ENOMEM);
  379. goto fail;
  380. }
  381. avio_get_str(pb, size, buffer, size + 1);
  382. if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
  383. goto fail;
  384. }
  385. break;
  386. case MKBETAG('S', '2', 'V', 'I'):
  387. if (f_stvi++ || !size) {
  388. ret = AVERROR(EINVAL);
  389. goto fail;
  390. }
  391. buffer = av_malloc(size);
  392. if (!buffer) {
  393. ret = AVERROR(ENOMEM);
  394. goto fail;
  395. }
  396. avio_get_str(pb, INT_MAX, buffer, size);
  397. av_set_options_string(codec, buffer, "=", ",");
  398. if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
  399. goto fail;
  400. break;
  401. case MKBETAG('S', '2', 'A', 'U'):
  402. if (f_stau++ || !size) {
  403. ret = AVERROR(EINVAL);
  404. goto fail;
  405. }
  406. buffer = av_malloc(size);
  407. if (!buffer) {
  408. ret = AVERROR(ENOMEM);
  409. goto fail;
  410. }
  411. avio_get_str(pb, INT_MAX, buffer, size);
  412. av_set_options_string(codec, buffer, "=", ",");
  413. if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
  414. goto fail;
  415. break;
  416. }
  417. avio_seek(pb, next, SEEK_SET);
  418. }
  419. /* get until end of block reached */
  420. while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
  421. avio_r8(pb);
  422. /* init packet demux */
  423. ffm->packet_ptr = ffm->packet;
  424. ffm->packet_end = ffm->packet;
  425. ffm->frame_offset = 0;
  426. ffm->dts = 0;
  427. ffm->read_state = READ_HEADER;
  428. ffm->first_packet = 1;
  429. return 0;
  430. fail:
  431. ffm_close(s);
  432. return ret;
  433. }
  434. static int ffm_read_header(AVFormatContext *s)
  435. {
  436. FFMContext *ffm = s->priv_data;
  437. AVStream *st;
  438. AVIOContext *pb = s->pb;
  439. AVCodecContext *codec;
  440. int i, nb_streams;
  441. uint32_t tag;
  442. /* header */
  443. tag = avio_rl32(pb);
  444. if (tag == MKTAG('F', 'F', 'M', '2'))
  445. return ffm2_read_header(s);
  446. if (tag != MKTAG('F', 'F', 'M', '1'))
  447. goto fail;
  448. ffm->packet_size = avio_rb32(pb);
  449. if (ffm->packet_size != FFM_PACKET_SIZE)
  450. goto fail;
  451. ffm->write_index = avio_rb64(pb);
  452. /* get also filesize */
  453. if (pb->seekable) {
  454. ffm->file_size = avio_size(pb);
  455. if (ffm->write_index && 0)
  456. adjust_write_index(s);
  457. } else {
  458. ffm->file_size = (UINT64_C(1) << 63) - 1;
  459. }
  460. nb_streams = avio_rb32(pb);
  461. avio_rb32(pb); /* total bitrate */
  462. /* read each stream */
  463. for(i=0;i<nb_streams;i++) {
  464. char rc_eq_buf[128];
  465. st = avformat_new_stream(s, NULL);
  466. if (!st)
  467. goto fail;
  468. avpriv_set_pts_info(st, 64, 1, 1000000);
  469. codec = st->codec;
  470. /* generic info */
  471. codec->codec_id = avio_rb32(pb);
  472. codec->codec_type = avio_r8(pb); /* codec_type */
  473. codec->bit_rate = avio_rb32(pb);
  474. codec->flags = avio_rb32(pb);
  475. codec->flags2 = avio_rb32(pb);
  476. codec->debug = avio_rb32(pb);
  477. /* specific info */
  478. switch(codec->codec_type) {
  479. case AVMEDIA_TYPE_VIDEO:
  480. codec->time_base.num = avio_rb32(pb);
  481. codec->time_base.den = avio_rb32(pb);
  482. if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
  483. av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
  484. codec->time_base.num, codec->time_base.den);
  485. goto fail;
  486. }
  487. codec->width = avio_rb16(pb);
  488. codec->height = avio_rb16(pb);
  489. codec->gop_size = avio_rb16(pb);
  490. codec->pix_fmt = avio_rb32(pb);
  491. codec->qmin = avio_r8(pb);
  492. codec->qmax = avio_r8(pb);
  493. codec->max_qdiff = avio_r8(pb);
  494. codec->qcompress = avio_rb16(pb) / 10000.0;
  495. codec->qblur = avio_rb16(pb) / 10000.0;
  496. codec->bit_rate_tolerance = avio_rb32(pb);
  497. avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  498. codec->rc_eq = av_strdup(rc_eq_buf);
  499. codec->rc_max_rate = avio_rb32(pb);
  500. codec->rc_min_rate = avio_rb32(pb);
  501. codec->rc_buffer_size = avio_rb32(pb);
  502. codec->i_quant_factor = av_int2double(avio_rb64(pb));
  503. codec->b_quant_factor = av_int2double(avio_rb64(pb));
  504. codec->i_quant_offset = av_int2double(avio_rb64(pb));
  505. codec->b_quant_offset = av_int2double(avio_rb64(pb));
  506. codec->dct_algo = avio_rb32(pb);
  507. codec->strict_std_compliance = avio_rb32(pb);
  508. codec->max_b_frames = avio_rb32(pb);
  509. codec->mpeg_quant = avio_rb32(pb);
  510. codec->intra_dc_precision = avio_rb32(pb);
  511. codec->me_method = avio_rb32(pb);
  512. codec->mb_decision = avio_rb32(pb);
  513. codec->nsse_weight = avio_rb32(pb);
  514. codec->frame_skip_cmp = avio_rb32(pb);
  515. codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
  516. codec->codec_tag = avio_rb32(pb);
  517. codec->thread_count = avio_r8(pb);
  518. codec->coder_type = avio_rb32(pb);
  519. codec->me_cmp = avio_rb32(pb);
  520. codec->me_subpel_quality = avio_rb32(pb);
  521. codec->me_range = avio_rb32(pb);
  522. codec->keyint_min = avio_rb32(pb);
  523. codec->scenechange_threshold = avio_rb32(pb);
  524. codec->b_frame_strategy = avio_rb32(pb);
  525. codec->qcompress = av_int2double(avio_rb64(pb));
  526. codec->qblur = av_int2double(avio_rb64(pb));
  527. codec->max_qdiff = avio_rb32(pb);
  528. codec->refs = avio_rb32(pb);
  529. break;
  530. case AVMEDIA_TYPE_AUDIO:
  531. codec->sample_rate = avio_rb32(pb);
  532. codec->channels = avio_rl16(pb);
  533. codec->frame_size = avio_rl16(pb);
  534. break;
  535. default:
  536. goto fail;
  537. }
  538. if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  539. if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
  540. return AVERROR(ENOMEM);
  541. }
  542. }
  543. /* get until end of block reached */
  544. while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
  545. avio_r8(pb);
  546. /* init packet demux */
  547. ffm->packet_ptr = ffm->packet;
  548. ffm->packet_end = ffm->packet;
  549. ffm->frame_offset = 0;
  550. ffm->dts = 0;
  551. ffm->read_state = READ_HEADER;
  552. ffm->first_packet = 1;
  553. return 0;
  554. fail:
  555. ffm_close(s);
  556. return -1;
  557. }
  558. /* return < 0 if eof */
  559. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  560. {
  561. int size;
  562. FFMContext *ffm = s->priv_data;
  563. int duration, ret;
  564. switch(ffm->read_state) {
  565. case READ_HEADER:
  566. if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
  567. return ret;
  568. ff_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  569. avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
  570. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  571. FRAME_HEADER_SIZE)
  572. return -1;
  573. if (ffm->header[1] & FLAG_DTS)
  574. if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
  575. return -1;
  576. ffm->read_state = READ_DATA;
  577. /* fall through */
  578. case READ_DATA:
  579. size = AV_RB24(ffm->header + 2);
  580. if ((ret = ffm_is_avail_data(s, size)) < 0)
  581. return ret;
  582. duration = AV_RB24(ffm->header + 5);
  583. if (av_new_packet(pkt, size) < 0) {
  584. return AVERROR(ENOMEM);
  585. }
  586. pkt->stream_index = ffm->header[0];
  587. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  588. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  589. av_packet_unref(pkt);
  590. ffm->read_state = READ_HEADER;
  591. return -1;
  592. }
  593. pkt->pos = avio_tell(s->pb);
  594. if (ffm->header[1] & FLAG_KEY_FRAME)
  595. pkt->flags |= AV_PKT_FLAG_KEY;
  596. ffm->read_state = READ_HEADER;
  597. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  598. /* bad case: desynchronized packet. we cancel all the packet loading */
  599. av_packet_unref(pkt);
  600. return -1;
  601. }
  602. pkt->pts = AV_RB64(ffm->header+8);
  603. if (ffm->header[1] & FLAG_DTS)
  604. pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
  605. else
  606. pkt->dts = pkt->pts;
  607. pkt->duration = duration;
  608. break;
  609. }
  610. return 0;
  611. }
  612. /* seek to a given time in the file. The file read pointer is
  613. positioned at or before pts. XXX: the following code is quite
  614. approximative */
  615. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  616. {
  617. FFMContext *ffm = s->priv_data;
  618. int64_t pos_min, pos_max, pos;
  619. int64_t pts_min, pts_max, pts;
  620. double pos1;
  621. ff_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  622. /* find the position using linear interpolation (better than
  623. dichotomy in typical cases) */
  624. if (ffm->write_index && ffm->write_index < ffm->file_size) {
  625. if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
  626. pos_min = FFM_PACKET_SIZE;
  627. pos_max = ffm->write_index - FFM_PACKET_SIZE;
  628. } else {
  629. pos_min = ffm->write_index;
  630. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  631. }
  632. } else {
  633. pos_min = FFM_PACKET_SIZE;
  634. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  635. }
  636. while (pos_min <= pos_max) {
  637. pts_min = get_dts(s, pos_min);
  638. pts_max = get_dts(s, pos_max);
  639. if (pts_min > wanted_pts || pts_max <= wanted_pts) {
  640. pos = pts_min > wanted_pts ? pos_min : pos_max;
  641. goto found;
  642. }
  643. /* linear interpolation */
  644. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  645. (double)(pts_max - pts_min);
  646. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  647. if (pos <= pos_min)
  648. pos = pos_min;
  649. else if (pos >= pos_max)
  650. pos = pos_max;
  651. pts = get_dts(s, pos);
  652. /* check if we are lucky */
  653. if (pts == wanted_pts) {
  654. goto found;
  655. } else if (pts > wanted_pts) {
  656. pos_max = pos - FFM_PACKET_SIZE;
  657. } else {
  658. pos_min = pos + FFM_PACKET_SIZE;
  659. }
  660. }
  661. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  662. found:
  663. if (ffm_seek1(s, pos) < 0)
  664. return -1;
  665. /* reset read state */
  666. ffm->read_state = READ_HEADER;
  667. ffm->packet_ptr = ffm->packet;
  668. ffm->packet_end = ffm->packet;
  669. ffm->first_packet = 1;
  670. return 0;
  671. }
  672. static int ffm_probe(AVProbeData *p)
  673. {
  674. if (
  675. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  676. (p->buf[3] == '1' || p->buf[3] == '2'))
  677. return AVPROBE_SCORE_MAX + 1;
  678. return 0;
  679. }
  680. static const AVOption options[] = {
  681. {"server_attached", NULL, offsetof(FFMContext, server_attached), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
  682. {"ffm_write_index", NULL, offsetof(FFMContext, write_index), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
  683. {"ffm_file_size", NULL, offsetof(FFMContext, file_size), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
  684. { NULL },
  685. };
  686. static const AVClass ffm_class = {
  687. .class_name = "ffm demuxer",
  688. .item_name = av_default_item_name,
  689. .option = options,
  690. .version = LIBAVUTIL_VERSION_INT,
  691. };
  692. AVInputFormat ff_ffm_demuxer = {
  693. .name = "ffm",
  694. .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
  695. .priv_data_size = sizeof(FFMContext),
  696. .read_probe = ffm_probe,
  697. .read_header = ffm_read_header,
  698. .read_packet = ffm_read_packet,
  699. .read_close = ffm_close,
  700. .read_seek = ffm_seek,
  701. .priv_class = &ffm_class,
  702. };