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.

875 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_append_recommended_configuration(AVStream *st, char **conf)
  224. {
  225. int ret;
  226. size_t newsize;
  227. av_assert0(conf && st);
  228. if (!*conf)
  229. return 0;
  230. if (!st->recommended_encoder_configuration) {
  231. st->recommended_encoder_configuration = *conf;
  232. *conf = 0;
  233. return 0;
  234. }
  235. newsize = strlen(*conf) + strlen(st->recommended_encoder_configuration) + 2;
  236. if ((ret = av_reallocp(&st->recommended_encoder_configuration, newsize)) < 0)
  237. return ret;
  238. av_strlcat(st->recommended_encoder_configuration, ",", newsize);
  239. av_strlcat(st->recommended_encoder_configuration, *conf, newsize);
  240. av_freep(conf);
  241. return 0;
  242. }
  243. #define VALIDATE_PARAMETER(parameter, name, check) { \
  244. if (check) { \
  245. av_log(s, AV_LOG_ERROR, "Invalid " name " %d\n", codecpar->parameter); \
  246. ret = AVERROR_INVALIDDATA; \
  247. goto fail; \
  248. } \
  249. }
  250. static int ffm2_read_header(AVFormatContext *s)
  251. {
  252. FFMContext *ffm = s->priv_data;
  253. AVStream *st;
  254. AVIOContext *pb = s->pb;
  255. AVCodecContext *dummy_codec = NULL;
  256. AVCodecParameters *codecpar;
  257. const AVCodecDescriptor *codec_desc;
  258. int ret;
  259. int f_main = 0, f_cprv = -1, f_stvi = -1, f_stau = -1;
  260. AVCodec *enc;
  261. char *buffer;
  262. ffm->packet_size = avio_rb32(pb);
  263. if (ffm->packet_size != FFM_PACKET_SIZE) {
  264. av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
  265. ffm->packet_size, FFM_PACKET_SIZE);
  266. ret = AVERROR_INVALIDDATA;
  267. goto fail;
  268. }
  269. ffm->write_index = avio_rb64(pb);
  270. /* get also filesize */
  271. if (pb->seekable) {
  272. ffm->file_size = avio_size(pb);
  273. if (ffm->write_index && 0)
  274. adjust_write_index(s);
  275. } else {
  276. ffm->file_size = (UINT64_C(1) << 63) - 1;
  277. }
  278. dummy_codec = avcodec_alloc_context3(NULL);
  279. while(!avio_feof(pb)) {
  280. unsigned id = avio_rb32(pb);
  281. unsigned size = avio_rb32(pb);
  282. int64_t next = avio_tell(pb) + size;
  283. char rc_eq_buf[128];
  284. int flags;
  285. if(!id)
  286. break;
  287. switch(id) {
  288. case MKBETAG('M', 'A', 'I', 'N'):
  289. if (f_main++) {
  290. ret = AVERROR(EINVAL);
  291. goto fail;
  292. }
  293. avio_rb32(pb); /* nb_streams */
  294. avio_rb32(pb); /* total bitrate */
  295. break;
  296. case MKBETAG('C', 'O', 'M', 'M'):
  297. f_cprv = f_stvi = f_stau = 0;
  298. st = avformat_new_stream(s, NULL);
  299. if (!st) {
  300. ret = AVERROR(ENOMEM);
  301. goto fail;
  302. }
  303. avpriv_set_pts_info(st, 64, 1, 1000000);
  304. codecpar = st->codecpar;
  305. /* generic info */
  306. codecpar->codec_id = avio_rb32(pb);
  307. codec_desc = avcodec_descriptor_get(codecpar->codec_id);
  308. if (!codec_desc) {
  309. av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codecpar->codec_id);
  310. codecpar->codec_id = AV_CODEC_ID_NONE;
  311. ret = AVERROR_INVALIDDATA;
  312. goto fail;
  313. }
  314. codecpar->codec_type = avio_r8(pb);
  315. if (codecpar->codec_type != codec_desc->type) {
  316. av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
  317. codec_desc->type, codecpar->codec_type);
  318. codecpar->codec_id = AV_CODEC_ID_NONE;
  319. codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
  320. ret = AVERROR_INVALIDDATA;
  321. goto fail;
  322. }
  323. codecpar->bit_rate = avio_rb32(pb);
  324. if (codecpar->bit_rate < 0) {
  325. av_log(s, AV_LOG_ERROR, "Invalid bit rate %"PRId64"\n", codecpar->bit_rate);
  326. ret = AVERROR_INVALIDDATA;
  327. goto fail;
  328. }
  329. flags = avio_rb32(pb);
  330. #if FF_API_LAVF_AVCTX
  331. FF_DISABLE_DEPRECATION_WARNINGS
  332. st->codec->flags = flags;
  333. FF_ENABLE_DEPRECATION_WARNINGS
  334. #endif
  335. avio_rb32(pb); // flags2
  336. avio_rb32(pb); // debug
  337. if (flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  338. int size = avio_rb32(pb);
  339. if (size < 0 || size >= FF_MAX_EXTRADATA_SIZE) {
  340. av_log(s, AV_LOG_ERROR, "Invalid extradata size %d\n", size);
  341. ret = AVERROR_INVALIDDATA;
  342. goto fail;
  343. }
  344. codecpar->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
  345. if (!codecpar->extradata)
  346. return AVERROR(ENOMEM);
  347. codecpar->extradata_size = size;
  348. avio_read(pb, codecpar->extradata, size);
  349. }
  350. break;
  351. case MKBETAG('S', 'T', 'V', 'I'):
  352. if (f_stvi++ || codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
  353. ret = AVERROR(EINVAL);
  354. goto fail;
  355. }
  356. avio_rb32(pb); // time_base.num
  357. avio_rb32(pb); // time_base.den
  358. codecpar->width = avio_rb16(pb);
  359. codecpar->height = avio_rb16(pb);
  360. ret = av_image_check_size(codecpar->width, codecpar->height, 0, s);
  361. if (ret < 0)
  362. goto fail;
  363. avio_rb16(pb); // gop_size
  364. codecpar->format = avio_rb32(pb);
  365. if (!av_pix_fmt_desc_get(codecpar->format)) {
  366. av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codecpar->format);
  367. codecpar->format = AV_PIX_FMT_NONE;
  368. ret = AVERROR_INVALIDDATA;
  369. goto fail;
  370. }
  371. avio_r8(pb); // qmin
  372. avio_r8(pb); // qmax
  373. avio_r8(pb); // max_qdiff
  374. avio_rb16(pb); // qcompress / 10000.0
  375. avio_rb16(pb); // qblur / 10000.0
  376. avio_rb32(pb); // bit_rate_tolerance
  377. avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  378. avio_rb32(pb); // rc_max_rate
  379. avio_rb32(pb); // rc_min_rate
  380. avio_rb32(pb); // rc_buffer_size
  381. avio_rb64(pb); // i_quant_factor
  382. avio_rb64(pb); // b_quant_factor
  383. avio_rb64(pb); // i_quant_offset
  384. avio_rb64(pb); // b_quant_offset
  385. avio_rb32(pb); // dct_algo
  386. avio_rb32(pb); // strict_std_compliance
  387. avio_rb32(pb); // max_b_frames
  388. avio_rb32(pb); // mpeg_quant
  389. avio_rb32(pb); // intra_dc_precision
  390. avio_rb32(pb); // me_method
  391. avio_rb32(pb); // mb_decision
  392. avio_rb32(pb); // nsse_weight
  393. avio_rb32(pb); // frame_skip_cmp
  394. avio_rb64(pb); // rc_buffer_aggressivity
  395. codecpar->codec_tag = avio_rb32(pb);
  396. avio_r8(pb); // thread_count
  397. avio_rb32(pb); // coder_type
  398. avio_rb32(pb); // me_cmp
  399. avio_rb32(pb); // me_subpel_quality
  400. avio_rb32(pb); // me_range
  401. avio_rb32(pb); // keyint_min
  402. avio_rb32(pb); // scenechange_threshold
  403. avio_rb32(pb); // b_frame_strategy
  404. avio_rb64(pb); // qcompress
  405. avio_rb64(pb); // qblur
  406. avio_rb32(pb); // max_qdiff
  407. avio_rb32(pb); // refs
  408. break;
  409. case MKBETAG('S', 'T', 'A', 'U'):
  410. if (f_stau++ || codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
  411. ret = AVERROR(EINVAL);
  412. goto fail;
  413. }
  414. codecpar->sample_rate = avio_rb32(pb);
  415. VALIDATE_PARAMETER(sample_rate, "sample rate", codecpar->sample_rate < 0)
  416. codecpar->channels = avio_rl16(pb);
  417. VALIDATE_PARAMETER(channels, "number of channels", codecpar->channels < 0)
  418. codecpar->frame_size = avio_rl16(pb);
  419. VALIDATE_PARAMETER(frame_size, "frame size", codecpar->frame_size < 0)
  420. break;
  421. case MKBETAG('C', 'P', 'R', 'V'):
  422. if (f_cprv++) {
  423. ret = AVERROR(EINVAL);
  424. goto fail;
  425. }
  426. enc = avcodec_find_encoder(codecpar->codec_id);
  427. if (enc && enc->priv_data_size && enc->priv_class) {
  428. buffer = av_malloc(size + 1);
  429. if (!buffer) {
  430. ret = AVERROR(ENOMEM);
  431. goto fail;
  432. }
  433. avio_get_str(pb, size, buffer, size + 1);
  434. if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
  435. goto fail;
  436. }
  437. break;
  438. case MKBETAG('S', '2', 'V', 'I'):
  439. if (f_stvi++ || !size || codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
  440. ret = AVERROR(EINVAL);
  441. goto fail;
  442. }
  443. buffer = av_malloc(size);
  444. if (!buffer) {
  445. ret = AVERROR(ENOMEM);
  446. goto fail;
  447. }
  448. avio_get_str(pb, INT_MAX, buffer, size);
  449. // The lack of AVOptions support in AVCodecParameters makes this back and forth copying needed
  450. avcodec_parameters_to_context(dummy_codec, codecpar);
  451. av_set_options_string(dummy_codec, buffer, "=", ",");
  452. avcodec_parameters_from_context(codecpar, dummy_codec);
  453. if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
  454. goto fail;
  455. break;
  456. case MKBETAG('S', '2', 'A', 'U'):
  457. if (f_stau++ || !size || codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
  458. ret = AVERROR(EINVAL);
  459. goto fail;
  460. }
  461. buffer = av_malloc(size);
  462. if (!buffer) {
  463. ret = AVERROR(ENOMEM);
  464. goto fail;
  465. }
  466. avio_get_str(pb, INT_MAX, buffer, size);
  467. // The lack of AVOptions support in AVCodecParameters makes this back and forth copying needed
  468. avcodec_parameters_to_context(dummy_codec, codecpar);
  469. av_set_options_string(dummy_codec, buffer, "=", ",");
  470. avcodec_parameters_from_context(codecpar, dummy_codec);
  471. if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
  472. goto fail;
  473. break;
  474. }
  475. avio_seek(pb, next, SEEK_SET);
  476. }
  477. /* get until end of block reached */
  478. while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
  479. avio_r8(pb);
  480. /* init packet demux */
  481. ffm->packet_ptr = ffm->packet;
  482. ffm->packet_end = ffm->packet;
  483. ffm->frame_offset = 0;
  484. ffm->dts = 0;
  485. ffm->read_state = READ_HEADER;
  486. ffm->first_packet = 1;
  487. avcodec_free_context(&dummy_codec);
  488. return 0;
  489. fail:
  490. avcodec_free_context(&dummy_codec);
  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 *dummy_codec = NULL;
  499. AVCodecParameters *codecpar;
  500. const AVCodecDescriptor *codec_desc;
  501. int i, nb_streams, ret;
  502. uint32_t tag;
  503. /* header */
  504. tag = avio_rl32(pb);
  505. if (tag == MKTAG('F', 'F', 'M', '2'))
  506. return ffm2_read_header(s);
  507. if (tag != MKTAG('F', 'F', 'M', '1')) {
  508. ret = AVERROR_INVALIDDATA;
  509. goto fail;
  510. }
  511. ffm->packet_size = avio_rb32(pb);
  512. if (ffm->packet_size != FFM_PACKET_SIZE) {
  513. ret = AVERROR_INVALIDDATA;
  514. goto fail;
  515. }
  516. ffm->write_index = avio_rb64(pb);
  517. /* get also filesize */
  518. if (pb->seekable) {
  519. ffm->file_size = avio_size(pb);
  520. if (ffm->write_index && 0)
  521. adjust_write_index(s);
  522. } else {
  523. ffm->file_size = (UINT64_C(1) << 63) - 1;
  524. }
  525. dummy_codec = avcodec_alloc_context3(NULL);
  526. nb_streams = avio_rb32(pb);
  527. avio_rb32(pb); /* total bitrate */
  528. /* read each stream */
  529. for(i=0;i<nb_streams;i++) {
  530. char rc_eq_buf[128];
  531. int flags;
  532. st = avformat_new_stream(s, NULL);
  533. if (!st) {
  534. ret = AVERROR(ENOMEM);
  535. goto fail;
  536. }
  537. avpriv_set_pts_info(st, 64, 1, 1000000);
  538. codecpar = st->codecpar;
  539. /* generic info */
  540. codecpar->codec_id = avio_rb32(pb);
  541. codec_desc = avcodec_descriptor_get(codecpar->codec_id);
  542. if (!codec_desc) {
  543. av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codecpar->codec_id);
  544. codecpar->codec_id = AV_CODEC_ID_NONE;
  545. ret = AVERROR_INVALIDDATA;
  546. goto fail;
  547. }
  548. codecpar->codec_type = avio_r8(pb); /* codec_type */
  549. if (codecpar->codec_type != codec_desc->type) {
  550. av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
  551. codec_desc->type, codecpar->codec_type);
  552. codecpar->codec_id = AV_CODEC_ID_NONE;
  553. codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
  554. ret = AVERROR_INVALIDDATA;
  555. goto fail;
  556. }
  557. codecpar->bit_rate = avio_rb32(pb);
  558. if (codecpar->bit_rate < 0) {
  559. av_log(s, AV_LOG_WARNING, "Invalid bit rate %"PRId64"\n", codecpar->bit_rate);
  560. ret = AVERROR_INVALIDDATA;
  561. goto fail;
  562. }
  563. flags = avio_rb32(pb);
  564. #if FF_API_LAVF_AVCTX
  565. FF_DISABLE_DEPRECATION_WARNINGS
  566. st->codec->flags = flags;
  567. FF_ENABLE_DEPRECATION_WARNINGS
  568. #endif
  569. avio_rb32(pb); // flags2
  570. avio_rb32(pb); // debug
  571. /* specific info */
  572. switch(codecpar->codec_type) {
  573. case AVMEDIA_TYPE_VIDEO:
  574. avio_rb32(pb); // time_base.num
  575. avio_rb32(pb); // time_base.den
  576. codecpar->width = avio_rb16(pb);
  577. codecpar->height = avio_rb16(pb);
  578. if ((ret = av_image_check_size(codecpar->width, codecpar->height, 0, s)) < 0)
  579. goto fail;
  580. avio_rb16(pb); // gop_size
  581. codecpar->format = avio_rb32(pb);
  582. if (!av_pix_fmt_desc_get(codecpar->format)) {
  583. av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codecpar->format);
  584. codecpar->format = AV_PIX_FMT_NONE;
  585. ret = AVERROR_INVALIDDATA;
  586. goto fail;
  587. }
  588. avio_r8(pb); // qmin
  589. avio_r8(pb); // qmax
  590. avio_r8(pb); // max_qdiff
  591. avio_rb16(pb); // qcompress / 10000.0
  592. avio_rb16(pb); // qblur / 10000.0
  593. avio_rb32(pb); // bit_rate_tolerance
  594. avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  595. avio_rb32(pb); // rc_max_rate
  596. avio_rb32(pb); // rc_min_rate
  597. avio_rb32(pb); // rc_buffer_size
  598. avio_rb64(pb); // i_quant_factor
  599. avio_rb64(pb); // b_quant_factor
  600. avio_rb64(pb); // i_quant_offset
  601. avio_rb64(pb); // b_quant_offset
  602. avio_rb32(pb); // dct_algo
  603. avio_rb32(pb); // strict_std_compliance
  604. avio_rb32(pb); // max_b_frames
  605. avio_rb32(pb); // mpeg_quant
  606. avio_rb32(pb); // intra_dc_precision
  607. avio_rb32(pb); // me_method
  608. avio_rb32(pb); // mb_decision
  609. avio_rb32(pb); // nsse_weight
  610. avio_rb32(pb); // frame_skip_cmp
  611. avio_rb64(pb); // rc_buffer_aggressivity
  612. codecpar->codec_tag = avio_rb32(pb);
  613. avio_r8(pb); // thread_count
  614. avio_rb32(pb); // coder_type
  615. avio_rb32(pb); // me_cmp
  616. avio_rb32(pb); // me_subpel_quality
  617. avio_rb32(pb); // me_range
  618. avio_rb32(pb); // keyint_min
  619. avio_rb32(pb); // scenechange_threshold
  620. avio_rb32(pb); // b_frame_strategy
  621. avio_rb64(pb); // qcompress
  622. avio_rb64(pb); // qblur
  623. avio_rb32(pb); // max_qdiff
  624. avio_rb32(pb); // refs
  625. break;
  626. case AVMEDIA_TYPE_AUDIO:
  627. codecpar->sample_rate = avio_rb32(pb);
  628. VALIDATE_PARAMETER(sample_rate, "sample rate", codecpar->sample_rate < 0)
  629. codecpar->channels = avio_rl16(pb);
  630. VALIDATE_PARAMETER(channels, "number of channels", codecpar->channels < 0)
  631. codecpar->frame_size = avio_rl16(pb);
  632. VALIDATE_PARAMETER(frame_size, "frame size", codecpar->frame_size < 0)
  633. break;
  634. default:
  635. ret = AVERROR_INVALIDDATA;
  636. goto fail;
  637. }
  638. if (flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  639. int size = avio_rb32(pb);
  640. if (size < 0 || size >= FF_MAX_EXTRADATA_SIZE) {
  641. av_log(s, AV_LOG_ERROR, "Invalid extradata size %d\n", size);
  642. ret = AVERROR_INVALIDDATA;
  643. goto fail;
  644. }
  645. codecpar->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
  646. if (!codecpar->extradata)
  647. return AVERROR(ENOMEM);
  648. codecpar->extradata_size = size;
  649. avio_read(pb, codecpar->extradata, size);
  650. }
  651. }
  652. /* get until end of block reached */
  653. while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
  654. avio_r8(pb);
  655. /* init packet demux */
  656. ffm->packet_ptr = ffm->packet;
  657. ffm->packet_end = ffm->packet;
  658. ffm->frame_offset = 0;
  659. ffm->dts = 0;
  660. ffm->read_state = READ_HEADER;
  661. ffm->first_packet = 1;
  662. avcodec_free_context(&dummy_codec);
  663. return 0;
  664. fail:
  665. avcodec_free_context(&dummy_codec);
  666. return ret;
  667. }
  668. /* return < 0 if eof */
  669. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  670. {
  671. int size;
  672. FFMContext *ffm = s->priv_data;
  673. int duration, ret;
  674. switch(ffm->read_state) {
  675. case READ_HEADER:
  676. if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
  677. return ret;
  678. ff_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  679. avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
  680. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  681. FRAME_HEADER_SIZE)
  682. return -1;
  683. if (ffm->header[1] & FLAG_DTS)
  684. if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
  685. return -1;
  686. ffm->read_state = READ_DATA;
  687. /* fall through */
  688. case READ_DATA:
  689. size = AV_RB24(ffm->header + 2);
  690. if ((ret = ffm_is_avail_data(s, size)) < 0)
  691. return ret;
  692. duration = AV_RB24(ffm->header + 5);
  693. if (av_new_packet(pkt, size) < 0) {
  694. return AVERROR(ENOMEM);
  695. }
  696. pkt->stream_index = ffm->header[0];
  697. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  698. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  699. av_packet_unref(pkt);
  700. ffm->read_state = READ_HEADER;
  701. return -1;
  702. }
  703. pkt->pos = avio_tell(s->pb);
  704. if (ffm->header[1] & FLAG_KEY_FRAME)
  705. pkt->flags |= AV_PKT_FLAG_KEY;
  706. ffm->read_state = READ_HEADER;
  707. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  708. /* bad case: desynchronized packet. we cancel all the packet loading */
  709. av_packet_unref(pkt);
  710. return -1;
  711. }
  712. pkt->pts = AV_RB64(ffm->header+8);
  713. if (ffm->header[1] & FLAG_DTS)
  714. pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
  715. else
  716. pkt->dts = pkt->pts;
  717. pkt->duration = duration;
  718. break;
  719. }
  720. return 0;
  721. }
  722. /* seek to a given time in the file. The file read pointer is
  723. positioned at or before pts. XXX: the following code is quite
  724. approximative */
  725. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  726. {
  727. FFMContext *ffm = s->priv_data;
  728. int64_t pos_min, pos_max, pos;
  729. int64_t pts_min, pts_max, pts;
  730. double pos1;
  731. ff_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  732. /* find the position using linear interpolation (better than
  733. dichotomy in typical cases) */
  734. if (ffm->write_index && ffm->write_index < ffm->file_size) {
  735. if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
  736. pos_min = FFM_PACKET_SIZE;
  737. pos_max = ffm->write_index - FFM_PACKET_SIZE;
  738. } else {
  739. pos_min = ffm->write_index;
  740. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  741. }
  742. } else {
  743. pos_min = FFM_PACKET_SIZE;
  744. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  745. }
  746. while (pos_min <= pos_max) {
  747. pts_min = get_dts(s, pos_min);
  748. pts_max = get_dts(s, pos_max);
  749. if (pts_min > wanted_pts || pts_max <= wanted_pts) {
  750. pos = pts_min > wanted_pts ? pos_min : pos_max;
  751. goto found;
  752. }
  753. /* linear interpolation */
  754. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  755. (double)(pts_max - pts_min);
  756. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  757. if (pos <= pos_min)
  758. pos = pos_min;
  759. else if (pos >= pos_max)
  760. pos = pos_max;
  761. pts = get_dts(s, pos);
  762. /* check if we are lucky */
  763. if (pts == wanted_pts) {
  764. goto found;
  765. } else if (pts > wanted_pts) {
  766. pos_max = pos - FFM_PACKET_SIZE;
  767. } else {
  768. pos_min = pos + FFM_PACKET_SIZE;
  769. }
  770. }
  771. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  772. found:
  773. if (ffm_seek1(s, pos) < 0)
  774. return -1;
  775. /* reset read state */
  776. ffm->read_state = READ_HEADER;
  777. ffm->packet_ptr = ffm->packet;
  778. ffm->packet_end = ffm->packet;
  779. ffm->first_packet = 1;
  780. return 0;
  781. }
  782. static int ffm_probe(AVProbeData *p)
  783. {
  784. if (
  785. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  786. (p->buf[3] == '1' || p->buf[3] == '2'))
  787. return AVPROBE_SCORE_MAX + 1;
  788. return 0;
  789. }
  790. static const AVOption options[] = {
  791. {"server_attached", NULL, offsetof(FFMContext, server_attached), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
  792. {"ffm_write_index", NULL, offsetof(FFMContext, write_index), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
  793. {"ffm_file_size", NULL, offsetof(FFMContext, file_size), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
  794. { NULL },
  795. };
  796. static const AVClass ffm_class = {
  797. .class_name = "ffm demuxer",
  798. .item_name = av_default_item_name,
  799. .option = options,
  800. .version = LIBAVUTIL_VERSION_INT,
  801. };
  802. AVInputFormat ff_ffm_demuxer = {
  803. .name = "ffm",
  804. .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
  805. .priv_data_size = sizeof(FFMContext),
  806. .read_probe = ffm_probe,
  807. .read_header = ffm_read_header,
  808. .read_packet = ffm_read_packet,
  809. .read_seek = ffm_seek,
  810. .priv_class = &ffm_class,
  811. };