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.

795 lines
26KB

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