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.

865 lines
30KB

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