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.

800 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 "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. ptrdiff_t 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 fill_size, size1, frame_offset;
  85. uint32_t id;
  86. ptrdiff_t len;
  87. int64_t last_pos = -1;
  88. size1 = size;
  89. while (size > 0) {
  90. redo:
  91. len = ffm->packet_end - ffm->packet_ptr;
  92. if (len < 0)
  93. return -1;
  94. if (len > size)
  95. len = size;
  96. if (len == 0) {
  97. if (avio_tell(pb) == ffm->file_size) {
  98. if (ffm->server_attached) {
  99. avio_seek(pb, ffm->packet_size, SEEK_SET);
  100. } else
  101. return AVERROR_EOF;
  102. }
  103. retry_read:
  104. if (pb->buffer_size != ffm->packet_size) {
  105. int64_t tell = avio_tell(pb);
  106. int ret = ffio_set_buf_size(pb, ffm->packet_size);
  107. if (ret < 0)
  108. return ret;
  109. avio_seek(pb, tell, SEEK_SET);
  110. }
  111. id = avio_rb16(pb); /* PACKET_ID */
  112. if (id != PACKET_ID) {
  113. if (ffm_resync(s, id) < 0)
  114. return -1;
  115. last_pos = avio_tell(pb);
  116. }
  117. fill_size = avio_rb16(pb);
  118. ffm->dts = avio_rb64(pb);
  119. frame_offset = avio_rb16(pb);
  120. avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
  121. if (ffm->packet_size < FFM_HEADER_SIZE + fill_size || frame_offset < 0) {
  122. return -1;
  123. }
  124. ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
  125. /* if first packet or resynchronization packet, we must
  126. handle it specifically */
  127. if (ffm->first_packet || (frame_offset & 0x8000)) {
  128. if (!frame_offset) {
  129. /* This packet has no frame headers in it */
  130. if (avio_tell(pb) >= ffm->packet_size * 3LL) {
  131. int64_t seekback = FFMIN(ffm->packet_size * 2LL, avio_tell(pb) - last_pos);
  132. seekback = FFMAX(seekback, 0);
  133. avio_seek(pb, -seekback, SEEK_CUR);
  134. goto retry_read;
  135. }
  136. /* This is bad, we cannot find a valid frame header */
  137. return 0;
  138. }
  139. ffm->first_packet = 0;
  140. if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE) {
  141. ffm->packet_end = ffm->packet_ptr;
  142. return -1;
  143. }
  144. ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
  145. if (!header)
  146. break;
  147. } else {
  148. ffm->packet_ptr = ffm->packet;
  149. }
  150. goto redo;
  151. }
  152. memcpy(buf, ffm->packet_ptr, len);
  153. buf += len;
  154. ffm->packet_ptr += len;
  155. size -= len;
  156. header = 0;
  157. }
  158. return size1 - size;
  159. }
  160. /* ensure that actual seeking happens between FFM_PACKET_SIZE
  161. and file_size - FFM_PACKET_SIZE */
  162. static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
  163. {
  164. FFMContext *ffm = s->priv_data;
  165. AVIOContext *pb = s->pb;
  166. int64_t pos;
  167. pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
  168. pos = FFMAX(pos, FFM_PACKET_SIZE);
  169. ff_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
  170. return avio_seek(pb, pos, SEEK_SET);
  171. }
  172. static int64_t get_dts(AVFormatContext *s, int64_t pos)
  173. {
  174. AVIOContext *pb = s->pb;
  175. int64_t dts;
  176. ffm_seek1(s, pos);
  177. avio_skip(pb, 4);
  178. dts = avio_rb64(pb);
  179. ff_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
  180. return dts;
  181. }
  182. static void adjust_write_index(AVFormatContext *s)
  183. {
  184. FFMContext *ffm = s->priv_data;
  185. AVIOContext *pb = s->pb;
  186. int64_t pts;
  187. //int64_t orig_write_index = ffm->write_index;
  188. int64_t pos_min, pos_max;
  189. int64_t pts_start;
  190. int64_t ptr = avio_tell(pb);
  191. pos_min = 0;
  192. pos_max = ffm->file_size - 2 * 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. goto end;
  197. ffm->write_index = FFM_PACKET_SIZE;
  198. pts_start = get_dts(s, pos_min);
  199. pts = get_dts(s, pos_max);
  200. if (pts - 100000 <= pts_start) {
  201. while (1) {
  202. int64_t newpos;
  203. int64_t newpts;
  204. newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
  205. if (newpos == pos_min)
  206. break;
  207. newpts = get_dts(s, newpos);
  208. if (newpts - 100000 <= pts) {
  209. pos_max = newpos;
  210. pts = newpts;
  211. } else {
  212. pos_min = newpos;
  213. }
  214. }
  215. ffm->write_index += pos_max;
  216. }
  217. end:
  218. avio_seek(pb, ptr, SEEK_SET);
  219. }
  220. static int ffm_close(AVFormatContext *s)
  221. {
  222. int i;
  223. for (i = 0; i < s->nb_streams; i++)
  224. av_freep(&s->streams[i]->codec->rc_eq);
  225. return 0;
  226. }
  227. static int ffm_append_recommended_configuration(AVStream *st, char **conf)
  228. {
  229. int ret;
  230. size_t newsize;
  231. av_assert0(conf && st);
  232. if (!*conf)
  233. return 0;
  234. if (!st->recommended_encoder_configuration) {
  235. st->recommended_encoder_configuration = *conf;
  236. *conf = 0;
  237. return 0;
  238. }
  239. newsize = strlen(*conf) + strlen(st->recommended_encoder_configuration) + 2;
  240. if ((ret = av_reallocp(&st->recommended_encoder_configuration, newsize)) < 0)
  241. return ret;
  242. av_strlcat(st->recommended_encoder_configuration, ",", newsize);
  243. av_strlcat(st->recommended_encoder_configuration, *conf, newsize);
  244. av_freep(conf);
  245. return 0;
  246. }
  247. static int ffm2_read_header(AVFormatContext *s)
  248. {
  249. FFMContext *ffm = s->priv_data;
  250. AVStream *st;
  251. AVIOContext *pb = s->pb;
  252. AVCodecContext *codec;
  253. const AVCodecDescriptor *codec_desc;
  254. int ret;
  255. int f_main = 0, f_cprv = -1, f_stvi = -1, f_stau = -1;
  256. AVCodec *enc;
  257. char *buffer;
  258. ffm->packet_size = avio_rb32(pb);
  259. if (ffm->packet_size != FFM_PACKET_SIZE) {
  260. av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
  261. ffm->packet_size, FFM_PACKET_SIZE);
  262. ret = AVERROR_INVALIDDATA;
  263. goto fail;
  264. }
  265. ffm->write_index = avio_rb64(pb);
  266. /* get also filesize */
  267. if (pb->seekable) {
  268. ffm->file_size = avio_size(pb);
  269. if (ffm->write_index && 0)
  270. adjust_write_index(s);
  271. } else {
  272. ffm->file_size = (UINT64_C(1) << 63) - 1;
  273. }
  274. while(!avio_feof(pb)) {
  275. unsigned id = avio_rb32(pb);
  276. unsigned size = avio_rb32(pb);
  277. int64_t next = avio_tell(pb) + size;
  278. char rc_eq_buf[128];
  279. if(!id)
  280. break;
  281. switch(id) {
  282. case MKBETAG('M', 'A', 'I', 'N'):
  283. if (f_main++) {
  284. ret = AVERROR(EINVAL);
  285. goto fail;
  286. }
  287. avio_rb32(pb); /* nb_streams */
  288. avio_rb32(pb); /* total bitrate */
  289. break;
  290. case MKBETAG('C', 'O', 'M', 'M'):
  291. f_cprv = f_stvi = f_stau = 0;
  292. st = avformat_new_stream(s, NULL);
  293. if (!st) {
  294. ret = AVERROR(ENOMEM);
  295. goto fail;
  296. }
  297. avpriv_set_pts_info(st, 64, 1, 1000000);
  298. codec = st->codec;
  299. /* generic info */
  300. codec->codec_id = avio_rb32(pb);
  301. codec_desc = avcodec_descriptor_get(codec->codec_id);
  302. if (!codec_desc) {
  303. av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codec->codec_id);
  304. codec->codec_id = AV_CODEC_ID_NONE;
  305. goto fail;
  306. }
  307. codec->codec_type = avio_r8(pb);
  308. if (codec->codec_type != codec_desc->type) {
  309. av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
  310. codec_desc->type, codec->codec_type);
  311. codec->codec_id = AV_CODEC_ID_NONE;
  312. codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
  313. goto fail;
  314. }
  315. codec->bit_rate = avio_rb32(pb);
  316. codec->flags = avio_rb32(pb);
  317. codec->flags2 = avio_rb32(pb);
  318. codec->debug = avio_rb32(pb);
  319. if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  320. if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0)
  321. return AVERROR(ENOMEM);
  322. }
  323. break;
  324. case MKBETAG('S', 'T', 'V', 'I'):
  325. if (f_stvi++) {
  326. ret = AVERROR(EINVAL);
  327. goto fail;
  328. }
  329. codec->time_base.num = avio_rb32(pb);
  330. codec->time_base.den = avio_rb32(pb);
  331. if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
  332. av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
  333. codec->time_base.num, codec->time_base.den);
  334. ret = AVERROR_INVALIDDATA;
  335. goto fail;
  336. }
  337. codec->width = avio_rb16(pb);
  338. codec->height = avio_rb16(pb);
  339. codec->gop_size = avio_rb16(pb);
  340. codec->pix_fmt = avio_rb32(pb);
  341. codec->qmin = avio_r8(pb);
  342. codec->qmax = avio_r8(pb);
  343. codec->max_qdiff = avio_r8(pb);
  344. codec->qcompress = avio_rb16(pb) / 10000.0;
  345. codec->qblur = avio_rb16(pb) / 10000.0;
  346. codec->bit_rate_tolerance = avio_rb32(pb);
  347. avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  348. codec->rc_eq = av_strdup(rc_eq_buf);
  349. codec->rc_max_rate = avio_rb32(pb);
  350. codec->rc_min_rate = avio_rb32(pb);
  351. codec->rc_buffer_size = avio_rb32(pb);
  352. codec->i_quant_factor = av_int2double(avio_rb64(pb));
  353. codec->b_quant_factor = av_int2double(avio_rb64(pb));
  354. codec->i_quant_offset = av_int2double(avio_rb64(pb));
  355. codec->b_quant_offset = av_int2double(avio_rb64(pb));
  356. codec->dct_algo = avio_rb32(pb);
  357. codec->strict_std_compliance = avio_rb32(pb);
  358. codec->max_b_frames = avio_rb32(pb);
  359. codec->mpeg_quant = avio_rb32(pb);
  360. codec->intra_dc_precision = avio_rb32(pb);
  361. codec->me_method = avio_rb32(pb);
  362. codec->mb_decision = avio_rb32(pb);
  363. codec->nsse_weight = avio_rb32(pb);
  364. codec->frame_skip_cmp = avio_rb32(pb);
  365. codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
  366. codec->codec_tag = avio_rb32(pb);
  367. codec->thread_count = avio_r8(pb);
  368. codec->coder_type = avio_rb32(pb);
  369. codec->me_cmp = avio_rb32(pb);
  370. codec->me_subpel_quality = avio_rb32(pb);
  371. codec->me_range = avio_rb32(pb);
  372. codec->keyint_min = avio_rb32(pb);
  373. codec->scenechange_threshold = avio_rb32(pb);
  374. codec->b_frame_strategy = avio_rb32(pb);
  375. codec->qcompress = av_int2double(avio_rb64(pb));
  376. codec->qblur = av_int2double(avio_rb64(pb));
  377. codec->max_qdiff = avio_rb32(pb);
  378. codec->refs = avio_rb32(pb);
  379. break;
  380. case MKBETAG('S', 'T', 'A', 'U'):
  381. if (f_stau++) {
  382. ret = AVERROR(EINVAL);
  383. goto fail;
  384. }
  385. codec->sample_rate = avio_rb32(pb);
  386. codec->channels = avio_rl16(pb);
  387. codec->frame_size = avio_rl16(pb);
  388. break;
  389. case MKBETAG('C', 'P', 'R', 'V'):
  390. if (f_cprv++) {
  391. ret = AVERROR(EINVAL);
  392. goto fail;
  393. }
  394. enc = avcodec_find_encoder(codec->codec_id);
  395. if (enc && enc->priv_data_size && enc->priv_class) {
  396. buffer = av_malloc(size + 1);
  397. if (!buffer) {
  398. ret = AVERROR(ENOMEM);
  399. goto fail;
  400. }
  401. avio_get_str(pb, size, buffer, size + 1);
  402. if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
  403. goto fail;
  404. }
  405. break;
  406. case MKBETAG('S', '2', 'V', 'I'):
  407. if (f_stvi++ || !size) {
  408. ret = AVERROR(EINVAL);
  409. goto fail;
  410. }
  411. buffer = av_malloc(size);
  412. if (!buffer) {
  413. ret = AVERROR(ENOMEM);
  414. goto fail;
  415. }
  416. avio_get_str(pb, INT_MAX, buffer, size);
  417. av_set_options_string(codec, buffer, "=", ",");
  418. if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
  419. goto fail;
  420. break;
  421. case MKBETAG('S', '2', 'A', 'U'):
  422. if (f_stau++ || !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. }
  437. avio_seek(pb, next, SEEK_SET);
  438. }
  439. /* get until end of block reached */
  440. while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
  441. avio_r8(pb);
  442. /* init packet demux */
  443. ffm->packet_ptr = ffm->packet;
  444. ffm->packet_end = ffm->packet;
  445. ffm->frame_offset = 0;
  446. ffm->dts = 0;
  447. ffm->read_state = READ_HEADER;
  448. ffm->first_packet = 1;
  449. return 0;
  450. fail:
  451. ffm_close(s);
  452. return ret;
  453. }
  454. static int ffm_read_header(AVFormatContext *s)
  455. {
  456. FFMContext *ffm = s->priv_data;
  457. AVStream *st;
  458. AVIOContext *pb = s->pb;
  459. AVCodecContext *codec;
  460. const AVCodecDescriptor *codec_desc;
  461. int i, nb_streams;
  462. uint32_t tag;
  463. /* header */
  464. tag = avio_rl32(pb);
  465. if (tag == MKTAG('F', 'F', 'M', '2'))
  466. return ffm2_read_header(s);
  467. if (tag != MKTAG('F', 'F', 'M', '1'))
  468. goto fail;
  469. ffm->packet_size = avio_rb32(pb);
  470. if (ffm->packet_size != FFM_PACKET_SIZE)
  471. goto fail;
  472. ffm->write_index = avio_rb64(pb);
  473. /* get also filesize */
  474. if (pb->seekable) {
  475. ffm->file_size = avio_size(pb);
  476. if (ffm->write_index && 0)
  477. adjust_write_index(s);
  478. } else {
  479. ffm->file_size = (UINT64_C(1) << 63) - 1;
  480. }
  481. nb_streams = avio_rb32(pb);
  482. avio_rb32(pb); /* total bitrate */
  483. /* read each stream */
  484. for(i=0;i<nb_streams;i++) {
  485. char rc_eq_buf[128];
  486. st = avformat_new_stream(s, NULL);
  487. if (!st)
  488. goto fail;
  489. avpriv_set_pts_info(st, 64, 1, 1000000);
  490. codec = st->codec;
  491. /* generic info */
  492. codec->codec_id = avio_rb32(pb);
  493. codec_desc = avcodec_descriptor_get(codec->codec_id);
  494. if (!codec_desc) {
  495. av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codec->codec_id);
  496. codec->codec_id = AV_CODEC_ID_NONE;
  497. goto fail;
  498. }
  499. codec->codec_type = avio_r8(pb); /* codec_type */
  500. if (codec->codec_type != codec_desc->type) {
  501. av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
  502. codec_desc->type, codec->codec_type);
  503. codec->codec_id = AV_CODEC_ID_NONE;
  504. codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
  505. goto fail;
  506. }
  507. codec->bit_rate = avio_rb32(pb);
  508. codec->flags = avio_rb32(pb);
  509. codec->flags2 = avio_rb32(pb);
  510. codec->debug = avio_rb32(pb);
  511. /* specific info */
  512. switch(codec->codec_type) {
  513. case AVMEDIA_TYPE_VIDEO:
  514. codec->time_base.num = avio_rb32(pb);
  515. codec->time_base.den = avio_rb32(pb);
  516. if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
  517. av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
  518. codec->time_base.num, codec->time_base.den);
  519. goto fail;
  520. }
  521. codec->width = avio_rb16(pb);
  522. codec->height = avio_rb16(pb);
  523. codec->gop_size = avio_rb16(pb);
  524. codec->pix_fmt = avio_rb32(pb);
  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_packet_unref(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_packet_unref(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. static const AVOption options[] = {
  715. {"server_attached", NULL, offsetof(FFMContext, server_attached), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
  716. {"ffm_write_index", NULL, offsetof(FFMContext, write_index), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
  717. {"ffm_file_size", NULL, offsetof(FFMContext, file_size), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
  718. { NULL },
  719. };
  720. static const AVClass ffm_class = {
  721. .class_name = "ffm demuxer",
  722. .item_name = av_default_item_name,
  723. .option = options,
  724. .version = LIBAVUTIL_VERSION_INT,
  725. };
  726. AVInputFormat ff_ffm_demuxer = {
  727. .name = "ffm",
  728. .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
  729. .priv_data_size = sizeof(FFMContext),
  730. .read_probe = ffm_probe,
  731. .read_header = ffm_read_header,
  732. .read_packet = ffm_read_packet,
  733. .read_close = ffm_close,
  734. .read_seek = ffm_seek,
  735. .priv_class = &ffm_class,
  736. };