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.

786 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 "libavutil/pixdesc.h"
  29. #include "avformat.h"
  30. #include "internal.h"
  31. #include "ffm.h"
  32. #include "avio_internal.h"
  33. static int ffm_is_avail_data(AVFormatContext *s, int size)
  34. {
  35. FFMContext *ffm = s->priv_data;
  36. int64_t pos, avail_size;
  37. int len;
  38. len = ffm->packet_end - ffm->packet_ptr;
  39. if (size <= len)
  40. return 1;
  41. pos = avio_tell(s->pb);
  42. if (!ffm->write_index) {
  43. if (pos == ffm->file_size)
  44. return AVERROR_EOF;
  45. avail_size = ffm->file_size - pos;
  46. } else {
  47. if (pos == ffm->write_index) {
  48. /* exactly at the end of stream */
  49. return AVERROR(EAGAIN);
  50. } else if (pos < ffm->write_index) {
  51. avail_size = ffm->write_index - pos;
  52. } else {
  53. avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
  54. }
  55. }
  56. avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
  57. if (size <= avail_size)
  58. return 1;
  59. else
  60. return AVERROR(EAGAIN);
  61. }
  62. static int ffm_resync(AVFormatContext *s, int state)
  63. {
  64. av_log(s, AV_LOG_ERROR, "resyncing\n");
  65. while (state != PACKET_ID) {
  66. if (avio_feof(s->pb)) {
  67. av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
  68. return -1;
  69. }
  70. state = (state << 8) | avio_r8(s->pb);
  71. }
  72. return 0;
  73. }
  74. /* first is true if we read the frame header */
  75. static int ffm_read_data(AVFormatContext *s,
  76. uint8_t *buf, int size, int header)
  77. {
  78. FFMContext *ffm = s->priv_data;
  79. AVIOContext *pb = s->pb;
  80. int len, fill_size, size1, frame_offset, id;
  81. int64_t last_pos = -1;
  82. size1 = size;
  83. while (size > 0) {
  84. redo:
  85. len = ffm->packet_end - ffm->packet_ptr;
  86. if (len < 0)
  87. return -1;
  88. if (len > size)
  89. len = size;
  90. if (len == 0) {
  91. if (avio_tell(pb) == ffm->file_size)
  92. avio_seek(pb, ffm->packet_size, SEEK_SET);
  93. retry_read:
  94. if (pb->buffer_size != ffm->packet_size) {
  95. int64_t tell = avio_tell(pb);
  96. int ret = ffio_set_buf_size(pb, ffm->packet_size);
  97. if (ret < 0)
  98. return ret;
  99. avio_seek(pb, tell, SEEK_SET);
  100. }
  101. id = avio_rb16(pb); /* PACKET_ID */
  102. if (id != PACKET_ID) {
  103. if (ffm_resync(s, id) < 0)
  104. return -1;
  105. last_pos = avio_tell(pb);
  106. }
  107. fill_size = avio_rb16(pb);
  108. ffm->dts = avio_rb64(pb);
  109. frame_offset = avio_rb16(pb);
  110. avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
  111. if (ffm->packet_size < FFM_HEADER_SIZE + fill_size || frame_offset < 0) {
  112. return -1;
  113. }
  114. ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
  115. /* if first packet or resynchronization packet, we must
  116. handle it specifically */
  117. if (ffm->first_packet || (frame_offset & 0x8000)) {
  118. if (!frame_offset) {
  119. /* This packet has no frame headers in it */
  120. if (avio_tell(pb) >= ffm->packet_size * 3LL) {
  121. int64_t seekback = FFMIN(ffm->packet_size * 2LL, avio_tell(pb) - last_pos);
  122. seekback = FFMAX(seekback, 0);
  123. avio_seek(pb, -seekback, SEEK_CUR);
  124. goto retry_read;
  125. }
  126. /* This is bad, we cannot find a valid frame header */
  127. return 0;
  128. }
  129. ffm->first_packet = 0;
  130. if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE) {
  131. ffm->packet_end = ffm->packet_ptr;
  132. return -1;
  133. }
  134. ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
  135. if (!header)
  136. break;
  137. } else {
  138. ffm->packet_ptr = ffm->packet;
  139. }
  140. goto redo;
  141. }
  142. memcpy(buf, ffm->packet_ptr, len);
  143. buf += len;
  144. ffm->packet_ptr += len;
  145. size -= len;
  146. header = 0;
  147. }
  148. return size1 - size;
  149. }
  150. /* ensure that actual seeking happens between FFM_PACKET_SIZE
  151. and file_size - FFM_PACKET_SIZE */
  152. static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
  153. {
  154. FFMContext *ffm = s->priv_data;
  155. AVIOContext *pb = s->pb;
  156. int64_t pos;
  157. pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
  158. pos = FFMAX(pos, FFM_PACKET_SIZE);
  159. ff_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
  160. return avio_seek(pb, pos, SEEK_SET);
  161. }
  162. static int64_t get_dts(AVFormatContext *s, int64_t pos)
  163. {
  164. AVIOContext *pb = s->pb;
  165. int64_t dts;
  166. ffm_seek1(s, pos);
  167. avio_skip(pb, 4);
  168. dts = avio_rb64(pb);
  169. ff_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
  170. return dts;
  171. }
  172. static void adjust_write_index(AVFormatContext *s)
  173. {
  174. FFMContext *ffm = s->priv_data;
  175. AVIOContext *pb = s->pb;
  176. int64_t pts;
  177. //int64_t orig_write_index = ffm->write_index;
  178. int64_t pos_min, pos_max;
  179. int64_t pts_start;
  180. int64_t ptr = avio_tell(pb);
  181. pos_min = 0;
  182. pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
  183. pts_start = get_dts(s, pos_min);
  184. pts = get_dts(s, pos_max);
  185. if (pts - 100000 > pts_start)
  186. goto end;
  187. ffm->write_index = 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. while (1) {
  192. int64_t newpos;
  193. int64_t newpts;
  194. newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
  195. if (newpos == pos_min)
  196. break;
  197. newpts = get_dts(s, newpos);
  198. if (newpts - 100000 <= pts) {
  199. pos_max = newpos;
  200. pts = newpts;
  201. } else {
  202. pos_min = newpos;
  203. }
  204. }
  205. ffm->write_index += pos_max;
  206. }
  207. end:
  208. avio_seek(pb, ptr, SEEK_SET);
  209. }
  210. static int ffm_close(AVFormatContext *s)
  211. {
  212. int i;
  213. for (i = 0; i < s->nb_streams; i++)
  214. av_freep(&s->streams[i]->codec->rc_eq);
  215. return 0;
  216. }
  217. static int ffm_append_recommended_configuration(AVStream *st, char **conf)
  218. {
  219. int ret;
  220. size_t newsize;
  221. av_assert0(conf && st);
  222. if (!*conf)
  223. return 0;
  224. if (!st->recommended_encoder_configuration) {
  225. st->recommended_encoder_configuration = *conf;
  226. *conf = 0;
  227. return 0;
  228. }
  229. newsize = strlen(*conf) + strlen(st->recommended_encoder_configuration) + 2;
  230. if ((ret = av_reallocp(&st->recommended_encoder_configuration, newsize)) < 0)
  231. return ret;
  232. av_strlcat(st->recommended_encoder_configuration, ",", newsize);
  233. av_strlcat(st->recommended_encoder_configuration, *conf, newsize);
  234. av_freep(conf);
  235. return 0;
  236. }
  237. static int ffm2_read_header(AVFormatContext *s)
  238. {
  239. FFMContext *ffm = s->priv_data;
  240. AVStream *st;
  241. AVIOContext *pb = s->pb;
  242. AVCodecContext *codec;
  243. const AVCodecDescriptor *codec_desc;
  244. int ret;
  245. int f_main = 0, f_cprv = -1, f_stvi = -1, f_stau = -1;
  246. AVCodec *enc;
  247. char *buffer;
  248. ffm->packet_size = avio_rb32(pb);
  249. if (ffm->packet_size != FFM_PACKET_SIZE) {
  250. av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
  251. ffm->packet_size, FFM_PACKET_SIZE);
  252. ret = AVERROR_INVALIDDATA;
  253. goto fail;
  254. }
  255. ffm->write_index = avio_rb64(pb);
  256. /* get also filesize */
  257. if (pb->seekable) {
  258. ffm->file_size = avio_size(pb);
  259. if (ffm->write_index && 0)
  260. adjust_write_index(s);
  261. } else {
  262. ffm->file_size = (UINT64_C(1) << 63) - 1;
  263. }
  264. while(!avio_feof(pb)) {
  265. unsigned id = avio_rb32(pb);
  266. unsigned size = avio_rb32(pb);
  267. int64_t next = avio_tell(pb) + size;
  268. char rc_eq_buf[128];
  269. if(!id)
  270. break;
  271. switch(id) {
  272. case MKBETAG('M', 'A', 'I', 'N'):
  273. if (f_main++) {
  274. ret = AVERROR(EINVAL);
  275. goto fail;
  276. }
  277. avio_rb32(pb); /* nb_streams */
  278. avio_rb32(pb); /* total bitrate */
  279. break;
  280. case MKBETAG('C', 'O', 'M', 'M'):
  281. f_cprv = f_stvi = f_stau = 0;
  282. st = avformat_new_stream(s, NULL);
  283. if (!st) {
  284. ret = AVERROR(ENOMEM);
  285. goto fail;
  286. }
  287. avpriv_set_pts_info(st, 64, 1, 1000000);
  288. codec = st->codec;
  289. /* generic info */
  290. codec->codec_id = avio_rb32(pb);
  291. codec_desc = avcodec_descriptor_get(codec->codec_id);
  292. if (!codec_desc) {
  293. av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codec->codec_id);
  294. codec->codec_id = AV_CODEC_ID_NONE;
  295. goto fail;
  296. }
  297. codec->codec_type = avio_r8(pb);
  298. if (codec->codec_type != codec_desc->type) {
  299. av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
  300. codec_desc->type, codec->codec_type);
  301. codec->codec_id = AV_CODEC_ID_NONE;
  302. codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
  303. goto fail;
  304. }
  305. codec->bit_rate = avio_rb32(pb);
  306. codec->flags = avio_rb32(pb);
  307. codec->flags2 = avio_rb32(pb);
  308. codec->debug = avio_rb32(pb);
  309. if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  310. if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
  311. return AVERROR(ENOMEM);
  312. }
  313. break;
  314. case MKBETAG('S', 'T', 'V', 'I'):
  315. if (f_stvi++ || codec->codec_type != AVMEDIA_TYPE_VIDEO) {
  316. ret = AVERROR(EINVAL);
  317. goto fail;
  318. }
  319. codec->time_base.num = avio_rb32(pb);
  320. codec->time_base.den = avio_rb32(pb);
  321. if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
  322. av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
  323. codec->time_base.num, codec->time_base.den);
  324. ret = AVERROR_INVALIDDATA;
  325. goto fail;
  326. }
  327. codec->width = avio_rb16(pb);
  328. codec->height = avio_rb16(pb);
  329. codec->gop_size = avio_rb16(pb);
  330. codec->pix_fmt = avio_rb32(pb);
  331. if (!av_pix_fmt_desc_get(codec->pix_fmt)) {
  332. av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codec->pix_fmt);
  333. codec->pix_fmt = AV_PIX_FMT_NONE;
  334. goto fail;
  335. }
  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++ || codec->codec_type != AVMEDIA_TYPE_AUDIO) {
  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 || codec->codec_type != AVMEDIA_TYPE_VIDEO) {
  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 || codec->codec_type != AVMEDIA_TYPE_AUDIO) {
  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. if (!av_pix_fmt_desc_get(codec->pix_fmt)) {
  521. av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codec->pix_fmt);
  522. codec->pix_fmt = AV_PIX_FMT_NONE;
  523. goto fail;
  524. }
  525. codec->qmin = avio_r8(pb);
  526. codec->qmax = avio_r8(pb);
  527. codec->max_qdiff = avio_r8(pb);
  528. codec->qcompress = avio_rb16(pb) / 10000.0;
  529. codec->qblur = avio_rb16(pb) / 10000.0;
  530. codec->bit_rate_tolerance = avio_rb32(pb);
  531. avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  532. codec->rc_eq = av_strdup(rc_eq_buf);
  533. codec->rc_max_rate = avio_rb32(pb);
  534. codec->rc_min_rate = avio_rb32(pb);
  535. codec->rc_buffer_size = avio_rb32(pb);
  536. codec->i_quant_factor = av_int2double(avio_rb64(pb));
  537. codec->b_quant_factor = av_int2double(avio_rb64(pb));
  538. codec->i_quant_offset = av_int2double(avio_rb64(pb));
  539. codec->b_quant_offset = av_int2double(avio_rb64(pb));
  540. codec->dct_algo = avio_rb32(pb);
  541. codec->strict_std_compliance = avio_rb32(pb);
  542. codec->max_b_frames = avio_rb32(pb);
  543. codec->mpeg_quant = avio_rb32(pb);
  544. codec->intra_dc_precision = avio_rb32(pb);
  545. codec->me_method = avio_rb32(pb);
  546. codec->mb_decision = avio_rb32(pb);
  547. codec->nsse_weight = avio_rb32(pb);
  548. codec->frame_skip_cmp = avio_rb32(pb);
  549. codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
  550. codec->codec_tag = avio_rb32(pb);
  551. codec->thread_count = avio_r8(pb);
  552. codec->coder_type = avio_rb32(pb);
  553. codec->me_cmp = avio_rb32(pb);
  554. codec->me_subpel_quality = avio_rb32(pb);
  555. codec->me_range = avio_rb32(pb);
  556. codec->keyint_min = avio_rb32(pb);
  557. codec->scenechange_threshold = avio_rb32(pb);
  558. codec->b_frame_strategy = avio_rb32(pb);
  559. codec->qcompress = av_int2double(avio_rb64(pb));
  560. codec->qblur = av_int2double(avio_rb64(pb));
  561. codec->max_qdiff = avio_rb32(pb);
  562. codec->refs = avio_rb32(pb);
  563. break;
  564. case AVMEDIA_TYPE_AUDIO:
  565. codec->sample_rate = avio_rb32(pb);
  566. codec->channels = avio_rl16(pb);
  567. codec->frame_size = avio_rl16(pb);
  568. break;
  569. default:
  570. goto fail;
  571. }
  572. if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  573. if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
  574. return AVERROR(ENOMEM);
  575. }
  576. }
  577. /* get until end of block reached */
  578. while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
  579. avio_r8(pb);
  580. /* init packet demux */
  581. ffm->packet_ptr = ffm->packet;
  582. ffm->packet_end = ffm->packet;
  583. ffm->frame_offset = 0;
  584. ffm->dts = 0;
  585. ffm->read_state = READ_HEADER;
  586. ffm->first_packet = 1;
  587. return 0;
  588. fail:
  589. ffm_close(s);
  590. return -1;
  591. }
  592. /* return < 0 if eof */
  593. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  594. {
  595. int size;
  596. FFMContext *ffm = s->priv_data;
  597. int duration, ret;
  598. switch(ffm->read_state) {
  599. case READ_HEADER:
  600. if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
  601. return ret;
  602. ff_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  603. avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
  604. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  605. FRAME_HEADER_SIZE)
  606. return -1;
  607. if (ffm->header[1] & FLAG_DTS)
  608. if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
  609. return -1;
  610. ffm->read_state = READ_DATA;
  611. /* fall through */
  612. case READ_DATA:
  613. size = AV_RB24(ffm->header + 2);
  614. if ((ret = ffm_is_avail_data(s, size)) < 0)
  615. return ret;
  616. duration = AV_RB24(ffm->header + 5);
  617. if (av_new_packet(pkt, size) < 0) {
  618. return AVERROR(ENOMEM);
  619. }
  620. pkt->stream_index = ffm->header[0];
  621. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  622. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  623. av_free_packet(pkt);
  624. ffm->read_state = READ_HEADER;
  625. return -1;
  626. }
  627. pkt->pos = avio_tell(s->pb);
  628. if (ffm->header[1] & FLAG_KEY_FRAME)
  629. pkt->flags |= AV_PKT_FLAG_KEY;
  630. ffm->read_state = READ_HEADER;
  631. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  632. /* bad case: desynchronized packet. we cancel all the packet loading */
  633. av_free_packet(pkt);
  634. return -1;
  635. }
  636. pkt->pts = AV_RB64(ffm->header+8);
  637. if (ffm->header[1] & FLAG_DTS)
  638. pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
  639. else
  640. pkt->dts = pkt->pts;
  641. pkt->duration = duration;
  642. break;
  643. }
  644. return 0;
  645. }
  646. /* seek to a given time in the file. The file read pointer is
  647. positioned at or before pts. XXX: the following code is quite
  648. approximative */
  649. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  650. {
  651. FFMContext *ffm = s->priv_data;
  652. int64_t pos_min, pos_max, pos;
  653. int64_t pts_min, pts_max, pts;
  654. double pos1;
  655. ff_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  656. /* find the position using linear interpolation (better than
  657. dichotomy in typical cases) */
  658. if (ffm->write_index && ffm->write_index < ffm->file_size) {
  659. if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
  660. pos_min = FFM_PACKET_SIZE;
  661. pos_max = ffm->write_index - FFM_PACKET_SIZE;
  662. } else {
  663. pos_min = ffm->write_index;
  664. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  665. }
  666. } else {
  667. pos_min = FFM_PACKET_SIZE;
  668. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  669. }
  670. while (pos_min <= pos_max) {
  671. pts_min = get_dts(s, pos_min);
  672. pts_max = get_dts(s, pos_max);
  673. if (pts_min > wanted_pts || pts_max <= wanted_pts) {
  674. pos = pts_min > wanted_pts ? pos_min : pos_max;
  675. goto found;
  676. }
  677. /* linear interpolation */
  678. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  679. (double)(pts_max - pts_min);
  680. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  681. if (pos <= pos_min)
  682. pos = pos_min;
  683. else if (pos >= pos_max)
  684. pos = pos_max;
  685. pts = get_dts(s, pos);
  686. /* check if we are lucky */
  687. if (pts == wanted_pts) {
  688. goto found;
  689. } else if (pts > wanted_pts) {
  690. pos_max = pos - FFM_PACKET_SIZE;
  691. } else {
  692. pos_min = pos + FFM_PACKET_SIZE;
  693. }
  694. }
  695. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  696. found:
  697. if (ffm_seek1(s, pos) < 0)
  698. return -1;
  699. /* reset read state */
  700. ffm->read_state = READ_HEADER;
  701. ffm->packet_ptr = ffm->packet;
  702. ffm->packet_end = ffm->packet;
  703. ffm->first_packet = 1;
  704. return 0;
  705. }
  706. static int ffm_probe(AVProbeData *p)
  707. {
  708. if (
  709. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  710. (p->buf[3] == '1' || p->buf[3] == '2'))
  711. return AVPROBE_SCORE_MAX + 1;
  712. return 0;
  713. }
  714. AVInputFormat ff_ffm_demuxer = {
  715. .name = "ffm",
  716. .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
  717. .priv_data_size = sizeof(FFMContext),
  718. .read_probe = ffm_probe,
  719. .read_header = ffm_read_header,
  720. .read_packet = ffm_read_packet,
  721. .read_close = ffm_close,
  722. .read_seek = ffm_seek,
  723. };