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.

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