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.

833 lines
28KB

  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, i;
  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. int size = avio_rb32(pb);
  322. codec->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
  323. if (!codec->extradata)
  324. return AVERROR(ENOMEM);
  325. codec->extradata_size = size;
  326. avio_read(pb, codec->extradata, size);
  327. }
  328. break;
  329. case MKBETAG('S', 'T', 'V', 'I'):
  330. if (f_stvi++) {
  331. ret = AVERROR(EINVAL);
  332. goto fail;
  333. }
  334. codec->time_base.num = avio_rb32(pb);
  335. codec->time_base.den = avio_rb32(pb);
  336. if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
  337. av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
  338. codec->time_base.num, codec->time_base.den);
  339. ret = AVERROR_INVALIDDATA;
  340. goto fail;
  341. }
  342. codec->width = avio_rb16(pb);
  343. codec->height = avio_rb16(pb);
  344. codec->gop_size = avio_rb16(pb);
  345. codec->pix_fmt = avio_rb32(pb);
  346. if (!av_pix_fmt_desc_get(codec->pix_fmt)) {
  347. av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codec->pix_fmt);
  348. codec->pix_fmt = AV_PIX_FMT_NONE;
  349. goto fail;
  350. }
  351. codec->qmin = avio_r8(pb);
  352. codec->qmax = avio_r8(pb);
  353. codec->max_qdiff = avio_r8(pb);
  354. codec->qcompress = avio_rb16(pb) / 10000.0;
  355. codec->qblur = avio_rb16(pb) / 10000.0;
  356. codec->bit_rate_tolerance = avio_rb32(pb);
  357. avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  358. codec->rc_eq = av_strdup(rc_eq_buf);
  359. codec->rc_max_rate = avio_rb32(pb);
  360. codec->rc_min_rate = avio_rb32(pb);
  361. codec->rc_buffer_size = avio_rb32(pb);
  362. codec->i_quant_factor = av_int2double(avio_rb64(pb));
  363. codec->b_quant_factor = av_int2double(avio_rb64(pb));
  364. codec->i_quant_offset = av_int2double(avio_rb64(pb));
  365. codec->b_quant_offset = av_int2double(avio_rb64(pb));
  366. codec->dct_algo = avio_rb32(pb);
  367. codec->strict_std_compliance = avio_rb32(pb);
  368. codec->max_b_frames = avio_rb32(pb);
  369. codec->mpeg_quant = avio_rb32(pb);
  370. codec->intra_dc_precision = avio_rb32(pb);
  371. codec->me_method = avio_rb32(pb);
  372. codec->mb_decision = avio_rb32(pb);
  373. codec->nsse_weight = avio_rb32(pb);
  374. codec->frame_skip_cmp = avio_rb32(pb);
  375. codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
  376. codec->codec_tag = avio_rb32(pb);
  377. codec->thread_count = avio_r8(pb);
  378. codec->coder_type = avio_rb32(pb);
  379. codec->me_cmp = avio_rb32(pb);
  380. codec->me_subpel_quality = avio_rb32(pb);
  381. codec->me_range = avio_rb32(pb);
  382. codec->keyint_min = avio_rb32(pb);
  383. codec->scenechange_threshold = avio_rb32(pb);
  384. codec->b_frame_strategy = avio_rb32(pb);
  385. codec->qcompress = av_int2double(avio_rb64(pb));
  386. codec->qblur = av_int2double(avio_rb64(pb));
  387. codec->max_qdiff = avio_rb32(pb);
  388. codec->refs = avio_rb32(pb);
  389. break;
  390. case MKBETAG('S', 'T', 'A', 'U'):
  391. if (f_stau++) {
  392. ret = AVERROR(EINVAL);
  393. goto fail;
  394. }
  395. codec->sample_rate = avio_rb32(pb);
  396. if (codec->sample_rate <= 0) {
  397. av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", codec->sample_rate);
  398. ret = AVERROR_INVALIDDATA;
  399. goto fail;
  400. }
  401. codec->channels = avio_rl16(pb);
  402. codec->frame_size = avio_rl16(pb);
  403. break;
  404. case MKBETAG('C', 'P', 'R', 'V'):
  405. if (f_cprv++) {
  406. ret = AVERROR(EINVAL);
  407. goto fail;
  408. }
  409. enc = avcodec_find_encoder(codec->codec_id);
  410. if (enc && enc->priv_data_size && enc->priv_class) {
  411. buffer = av_malloc(size + 1);
  412. if (!buffer) {
  413. ret = AVERROR(ENOMEM);
  414. goto fail;
  415. }
  416. avio_get_str(pb, size, buffer, size + 1);
  417. if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
  418. goto fail;
  419. }
  420. break;
  421. case MKBETAG('S', '2', 'V', 'I'):
  422. if (f_stvi++ || !size) {
  423. ret = AVERROR(EINVAL);
  424. goto fail;
  425. }
  426. buffer = av_malloc(size);
  427. if (!buffer) {
  428. ret = AVERROR(ENOMEM);
  429. goto fail;
  430. }
  431. avio_get_str(pb, INT_MAX, buffer, size);
  432. av_set_options_string(codec, buffer, "=", ",");
  433. if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
  434. goto fail;
  435. break;
  436. case MKBETAG('S', '2', 'A', 'U'):
  437. if (f_stau++ || !size) {
  438. ret = AVERROR(EINVAL);
  439. goto fail;
  440. }
  441. buffer = av_malloc(size);
  442. if (!buffer) {
  443. ret = AVERROR(ENOMEM);
  444. goto fail;
  445. }
  446. avio_get_str(pb, INT_MAX, buffer, size);
  447. av_set_options_string(codec, buffer, "=", ",");
  448. if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
  449. goto fail;
  450. break;
  451. }
  452. avio_seek(pb, next, SEEK_SET);
  453. }
  454. for (i = 0; i < s->nb_streams; i++)
  455. avcodec_parameters_from_context(s->streams[i]->codecpar, s->streams[i]->codec);
  456. /* get until end of block reached */
  457. while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
  458. avio_r8(pb);
  459. /* init packet demux */
  460. ffm->packet_ptr = ffm->packet;
  461. ffm->packet_end = ffm->packet;
  462. ffm->frame_offset = 0;
  463. ffm->dts = 0;
  464. ffm->read_state = READ_HEADER;
  465. ffm->first_packet = 1;
  466. return 0;
  467. fail:
  468. ffm_close(s);
  469. return ret;
  470. }
  471. static int ffm_read_header(AVFormatContext *s)
  472. {
  473. FFMContext *ffm = s->priv_data;
  474. AVStream *st;
  475. AVIOContext *pb = s->pb;
  476. AVCodecContext *codec;
  477. const AVCodecDescriptor *codec_desc;
  478. int i, nb_streams;
  479. uint32_t tag;
  480. /* header */
  481. tag = avio_rl32(pb);
  482. if (tag == MKTAG('F', 'F', 'M', '2'))
  483. return ffm2_read_header(s);
  484. if (tag != MKTAG('F', 'F', 'M', '1'))
  485. goto fail;
  486. ffm->packet_size = avio_rb32(pb);
  487. if (ffm->packet_size != FFM_PACKET_SIZE)
  488. goto fail;
  489. ffm->write_index = avio_rb64(pb);
  490. /* get also filesize */
  491. if (pb->seekable) {
  492. ffm->file_size = avio_size(pb);
  493. if (ffm->write_index && 0)
  494. adjust_write_index(s);
  495. } else {
  496. ffm->file_size = (UINT64_C(1) << 63) - 1;
  497. }
  498. nb_streams = avio_rb32(pb);
  499. avio_rb32(pb); /* total bitrate */
  500. /* read each stream */
  501. for(i=0;i<nb_streams;i++) {
  502. char rc_eq_buf[128];
  503. st = avformat_new_stream(s, NULL);
  504. if (!st)
  505. goto fail;
  506. avpriv_set_pts_info(st, 64, 1, 1000000);
  507. codec = st->codec;
  508. /* generic info */
  509. codec->codec_id = avio_rb32(pb);
  510. codec_desc = avcodec_descriptor_get(codec->codec_id);
  511. if (!codec_desc) {
  512. av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codec->codec_id);
  513. codec->codec_id = AV_CODEC_ID_NONE;
  514. goto fail;
  515. }
  516. codec->codec_type = avio_r8(pb); /* codec_type */
  517. if (codec->codec_type != codec_desc->type) {
  518. av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
  519. codec_desc->type, codec->codec_type);
  520. codec->codec_id = AV_CODEC_ID_NONE;
  521. codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
  522. goto fail;
  523. }
  524. codec->bit_rate = avio_rb32(pb);
  525. codec->flags = avio_rb32(pb);
  526. codec->flags2 = avio_rb32(pb);
  527. codec->debug = avio_rb32(pb);
  528. /* specific info */
  529. switch(codec->codec_type) {
  530. case AVMEDIA_TYPE_VIDEO:
  531. codec->time_base.num = avio_rb32(pb);
  532. codec->time_base.den = avio_rb32(pb);
  533. if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
  534. av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
  535. codec->time_base.num, codec->time_base.den);
  536. goto fail;
  537. }
  538. codec->width = avio_rb16(pb);
  539. codec->height = avio_rb16(pb);
  540. codec->gop_size = avio_rb16(pb);
  541. codec->pix_fmt = avio_rb32(pb);
  542. if (!av_pix_fmt_desc_get(codec->pix_fmt)) {
  543. av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codec->pix_fmt);
  544. codec->pix_fmt = AV_PIX_FMT_NONE;
  545. goto fail;
  546. }
  547. codec->qmin = avio_r8(pb);
  548. codec->qmax = avio_r8(pb);
  549. codec->max_qdiff = avio_r8(pb);
  550. codec->qcompress = avio_rb16(pb) / 10000.0;
  551. codec->qblur = avio_rb16(pb) / 10000.0;
  552. codec->bit_rate_tolerance = avio_rb32(pb);
  553. avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  554. codec->rc_eq = av_strdup(rc_eq_buf);
  555. codec->rc_max_rate = avio_rb32(pb);
  556. codec->rc_min_rate = avio_rb32(pb);
  557. codec->rc_buffer_size = avio_rb32(pb);
  558. codec->i_quant_factor = av_int2double(avio_rb64(pb));
  559. codec->b_quant_factor = av_int2double(avio_rb64(pb));
  560. codec->i_quant_offset = av_int2double(avio_rb64(pb));
  561. codec->b_quant_offset = av_int2double(avio_rb64(pb));
  562. codec->dct_algo = avio_rb32(pb);
  563. codec->strict_std_compliance = avio_rb32(pb);
  564. codec->max_b_frames = avio_rb32(pb);
  565. codec->mpeg_quant = avio_rb32(pb);
  566. codec->intra_dc_precision = avio_rb32(pb);
  567. codec->me_method = avio_rb32(pb);
  568. codec->mb_decision = avio_rb32(pb);
  569. codec->nsse_weight = avio_rb32(pb);
  570. codec->frame_skip_cmp = avio_rb32(pb);
  571. codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
  572. codec->codec_tag = avio_rb32(pb);
  573. codec->thread_count = avio_r8(pb);
  574. codec->coder_type = avio_rb32(pb);
  575. codec->me_cmp = avio_rb32(pb);
  576. codec->me_subpel_quality = avio_rb32(pb);
  577. codec->me_range = avio_rb32(pb);
  578. codec->keyint_min = avio_rb32(pb);
  579. codec->scenechange_threshold = avio_rb32(pb);
  580. codec->b_frame_strategy = avio_rb32(pb);
  581. codec->qcompress = av_int2double(avio_rb64(pb));
  582. codec->qblur = av_int2double(avio_rb64(pb));
  583. codec->max_qdiff = avio_rb32(pb);
  584. codec->refs = avio_rb32(pb);
  585. break;
  586. case AVMEDIA_TYPE_AUDIO:
  587. codec->sample_rate = avio_rb32(pb);
  588. if (codec->sample_rate <= 0) {
  589. av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", codec->sample_rate);
  590. goto fail;
  591. }
  592. codec->channels = avio_rl16(pb);
  593. codec->frame_size = avio_rl16(pb);
  594. break;
  595. default:
  596. goto fail;
  597. }
  598. if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  599. int size = avio_rb32(pb);
  600. codec->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
  601. if (!codec->extradata)
  602. return AVERROR(ENOMEM);
  603. codec->extradata_size = size;
  604. avio_read(pb, codec->extradata, size);
  605. }
  606. avcodec_parameters_from_context(st->codecpar, codec);
  607. }
  608. /* get until end of block reached */
  609. while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
  610. avio_r8(pb);
  611. /* init packet demux */
  612. ffm->packet_ptr = ffm->packet;
  613. ffm->packet_end = ffm->packet;
  614. ffm->frame_offset = 0;
  615. ffm->dts = 0;
  616. ffm->read_state = READ_HEADER;
  617. ffm->first_packet = 1;
  618. return 0;
  619. fail:
  620. ffm_close(s);
  621. return -1;
  622. }
  623. /* return < 0 if eof */
  624. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  625. {
  626. int size;
  627. FFMContext *ffm = s->priv_data;
  628. int duration, ret;
  629. switch(ffm->read_state) {
  630. case READ_HEADER:
  631. if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
  632. return ret;
  633. ff_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  634. avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
  635. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  636. FRAME_HEADER_SIZE)
  637. return -1;
  638. if (ffm->header[1] & FLAG_DTS)
  639. if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
  640. return -1;
  641. ffm->read_state = READ_DATA;
  642. /* fall through */
  643. case READ_DATA:
  644. size = AV_RB24(ffm->header + 2);
  645. if ((ret = ffm_is_avail_data(s, size)) < 0)
  646. return ret;
  647. duration = AV_RB24(ffm->header + 5);
  648. if (av_new_packet(pkt, size) < 0) {
  649. return AVERROR(ENOMEM);
  650. }
  651. pkt->stream_index = ffm->header[0];
  652. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  653. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  654. av_packet_unref(pkt);
  655. ffm->read_state = READ_HEADER;
  656. return -1;
  657. }
  658. pkt->pos = avio_tell(s->pb);
  659. if (ffm->header[1] & FLAG_KEY_FRAME)
  660. pkt->flags |= AV_PKT_FLAG_KEY;
  661. ffm->read_state = READ_HEADER;
  662. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  663. /* bad case: desynchronized packet. we cancel all the packet loading */
  664. av_packet_unref(pkt);
  665. return -1;
  666. }
  667. pkt->pts = AV_RB64(ffm->header+8);
  668. if (ffm->header[1] & FLAG_DTS)
  669. pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
  670. else
  671. pkt->dts = pkt->pts;
  672. pkt->duration = duration;
  673. break;
  674. }
  675. return 0;
  676. }
  677. /* seek to a given time in the file. The file read pointer is
  678. positioned at or before pts. XXX: the following code is quite
  679. approximative */
  680. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  681. {
  682. FFMContext *ffm = s->priv_data;
  683. int64_t pos_min, pos_max, pos;
  684. int64_t pts_min, pts_max, pts;
  685. double pos1;
  686. ff_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  687. /* find the position using linear interpolation (better than
  688. dichotomy in typical cases) */
  689. if (ffm->write_index && ffm->write_index < ffm->file_size) {
  690. if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
  691. pos_min = FFM_PACKET_SIZE;
  692. pos_max = ffm->write_index - FFM_PACKET_SIZE;
  693. } else {
  694. pos_min = ffm->write_index;
  695. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  696. }
  697. } else {
  698. pos_min = FFM_PACKET_SIZE;
  699. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  700. }
  701. while (pos_min <= pos_max) {
  702. pts_min = get_dts(s, pos_min);
  703. pts_max = get_dts(s, pos_max);
  704. if (pts_min > wanted_pts || pts_max <= wanted_pts) {
  705. pos = pts_min > wanted_pts ? pos_min : pos_max;
  706. goto found;
  707. }
  708. /* linear interpolation */
  709. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  710. (double)(pts_max - pts_min);
  711. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  712. if (pos <= pos_min)
  713. pos = pos_min;
  714. else if (pos >= pos_max)
  715. pos = pos_max;
  716. pts = get_dts(s, pos);
  717. /* check if we are lucky */
  718. if (pts == wanted_pts) {
  719. goto found;
  720. } else if (pts > wanted_pts) {
  721. pos_max = pos - FFM_PACKET_SIZE;
  722. } else {
  723. pos_min = pos + FFM_PACKET_SIZE;
  724. }
  725. }
  726. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  727. found:
  728. if (ffm_seek1(s, pos) < 0)
  729. return -1;
  730. /* reset read state */
  731. ffm->read_state = READ_HEADER;
  732. ffm->packet_ptr = ffm->packet;
  733. ffm->packet_end = ffm->packet;
  734. ffm->first_packet = 1;
  735. return 0;
  736. }
  737. static int ffm_probe(AVProbeData *p)
  738. {
  739. if (
  740. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  741. (p->buf[3] == '1' || p->buf[3] == '2'))
  742. return AVPROBE_SCORE_MAX + 1;
  743. return 0;
  744. }
  745. static const AVOption options[] = {
  746. {"server_attached", NULL, offsetof(FFMContext, server_attached), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
  747. {"ffm_write_index", NULL, offsetof(FFMContext, write_index), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
  748. {"ffm_file_size", NULL, offsetof(FFMContext, file_size), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
  749. { NULL },
  750. };
  751. static const AVClass ffm_class = {
  752. .class_name = "ffm demuxer",
  753. .item_name = av_default_item_name,
  754. .option = options,
  755. .version = LIBAVUTIL_VERSION_INT,
  756. };
  757. AVInputFormat ff_ffm_demuxer = {
  758. .name = "ffm",
  759. .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
  760. .priv_data_size = sizeof(FFMContext),
  761. .read_probe = ffm_probe,
  762. .read_header = ffm_read_header,
  763. .read_packet = ffm_read_packet,
  764. .read_close = ffm_close,
  765. .read_seek = ffm_seek,
  766. .priv_class = &ffm_class,
  767. };