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.

811 lines
27KB

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