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.

863 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(codec, 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 *codec, *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. if(!id)
  285. break;
  286. switch(id) {
  287. case MKBETAG('M', 'A', 'I', 'N'):
  288. if (f_main++) {
  289. ret = AVERROR(EINVAL);
  290. goto fail;
  291. }
  292. avio_rb32(pb); /* nb_streams */
  293. avio_rb32(pb); /* total bitrate */
  294. break;
  295. case MKBETAG('C', 'O', 'M', 'M'):
  296. f_cprv = f_stvi = f_stau = 0;
  297. st = avformat_new_stream(s, NULL);
  298. if (!st) {
  299. ret = AVERROR(ENOMEM);
  300. goto fail;
  301. }
  302. avpriv_set_pts_info(st, 64, 1, 1000000);
  303. codec = st->codec;
  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(codec, AV_LOG_ERROR, "Invalid bit rate %"PRId64"\n", codecpar->bit_rate);
  326. ret = AVERROR_INVALIDDATA;
  327. goto fail;
  328. }
  329. codec->flags = avio_rb32(pb);
  330. codec->flags2 = avio_rb32(pb);
  331. codec->debug = avio_rb32(pb);
  332. if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  333. int size = avio_rb32(pb);
  334. if (size < 0 || size >= FF_MAX_EXTRADATA_SIZE) {
  335. av_log(s, AV_LOG_ERROR, "Invalid extradata size %d\n", size);
  336. ret = AVERROR_INVALIDDATA;
  337. goto fail;
  338. }
  339. codecpar->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
  340. if (!codecpar->extradata)
  341. return AVERROR(ENOMEM);
  342. codecpar->extradata_size = size;
  343. avio_read(pb, codecpar->extradata, size);
  344. }
  345. break;
  346. case MKBETAG('S', 'T', 'V', 'I'):
  347. if (f_stvi++ || codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
  348. ret = AVERROR(EINVAL);
  349. goto fail;
  350. }
  351. codec->time_base.num = avio_rb32(pb);
  352. codec->time_base.den = avio_rb32(pb);
  353. if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
  354. av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
  355. codec->time_base.num, codec->time_base.den);
  356. ret = AVERROR_INVALIDDATA;
  357. goto fail;
  358. }
  359. codecpar->width = avio_rb16(pb);
  360. codecpar->height = avio_rb16(pb);
  361. ret = av_image_check_size(codecpar->width, codecpar->height, 0, s);
  362. if (ret < 0)
  363. goto fail;
  364. avio_rb16(pb); // gop_size
  365. codecpar->format = avio_rb32(pb);
  366. if (!av_pix_fmt_desc_get(codecpar->format)) {
  367. av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codecpar->format);
  368. codecpar->format = AV_PIX_FMT_NONE;
  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 *codec, *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. goto fail;
  509. ffm->packet_size = avio_rb32(pb);
  510. if (ffm->packet_size != FFM_PACKET_SIZE)
  511. goto fail;
  512. ffm->write_index = avio_rb64(pb);
  513. /* get also filesize */
  514. if (pb->seekable) {
  515. ffm->file_size = avio_size(pb);
  516. if (ffm->write_index && 0)
  517. adjust_write_index(s);
  518. } else {
  519. ffm->file_size = (UINT64_C(1) << 63) - 1;
  520. }
  521. dummy_codec = avcodec_alloc_context3(NULL);
  522. nb_streams = avio_rb32(pb);
  523. avio_rb32(pb); /* total bitrate */
  524. /* read each stream */
  525. for(i=0;i<nb_streams;i++) {
  526. char rc_eq_buf[128];
  527. st = avformat_new_stream(s, NULL);
  528. if (!st)
  529. goto fail;
  530. avpriv_set_pts_info(st, 64, 1, 1000000);
  531. codec = st->codec;
  532. codecpar = st->codecpar;
  533. /* generic info */
  534. codecpar->codec_id = avio_rb32(pb);
  535. codec_desc = avcodec_descriptor_get(codecpar->codec_id);
  536. if (!codec_desc) {
  537. av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codecpar->codec_id);
  538. codecpar->codec_id = AV_CODEC_ID_NONE;
  539. goto fail;
  540. }
  541. codecpar->codec_type = avio_r8(pb); /* codec_type */
  542. if (codecpar->codec_type != codec_desc->type) {
  543. av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
  544. codec_desc->type, codecpar->codec_type);
  545. codecpar->codec_id = AV_CODEC_ID_NONE;
  546. codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
  547. goto fail;
  548. }
  549. codecpar->bit_rate = avio_rb32(pb);
  550. if (codecpar->bit_rate < 0) {
  551. av_log(codec, AV_LOG_WARNING, "Invalid bit rate %"PRId64"\n", codecpar->bit_rate);
  552. goto fail;
  553. }
  554. codec->flags = avio_rb32(pb);
  555. codec->flags2 = avio_rb32(pb);
  556. codec->debug = avio_rb32(pb);
  557. /* specific info */
  558. switch(codecpar->codec_type) {
  559. case AVMEDIA_TYPE_VIDEO:
  560. codec->time_base.num = avio_rb32(pb);
  561. codec->time_base.den = avio_rb32(pb);
  562. if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
  563. av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
  564. codec->time_base.num, codec->time_base.den);
  565. goto fail;
  566. }
  567. codecpar->width = avio_rb16(pb);
  568. codecpar->height = avio_rb16(pb);
  569. if (av_image_check_size(codecpar->width, codecpar->height, 0, s) < 0)
  570. goto fail;
  571. avio_rb16(pb); // gop_size
  572. codecpar->format = avio_rb32(pb);
  573. if (!av_pix_fmt_desc_get(codecpar->format)) {
  574. av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codecpar->format);
  575. codecpar->format = AV_PIX_FMT_NONE;
  576. goto fail;
  577. }
  578. avio_r8(pb); // qmin
  579. avio_r8(pb); // qmax
  580. avio_r8(pb); // max_qdiff
  581. avio_rb16(pb); // qcompress / 10000.0
  582. avio_rb16(pb); // qblur / 10000.0
  583. avio_rb32(pb); // bit_rate_tolerance
  584. avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
  585. avio_rb32(pb); // rc_max_rate
  586. avio_rb32(pb); // rc_min_rate
  587. avio_rb32(pb); // rc_buffer_size
  588. avio_rb64(pb); // i_quant_factor
  589. avio_rb64(pb); // b_quant_factor
  590. avio_rb64(pb); // i_quant_offset
  591. avio_rb64(pb); // b_quant_offset
  592. avio_rb32(pb); // dct_algo
  593. avio_rb32(pb); // strict_std_compliance
  594. avio_rb32(pb); // max_b_frames
  595. avio_rb32(pb); // mpeg_quant
  596. avio_rb32(pb); // intra_dc_precision
  597. avio_rb32(pb); // me_method
  598. avio_rb32(pb); // mb_decision
  599. avio_rb32(pb); // nsse_weight
  600. avio_rb32(pb); // frame_skip_cmp
  601. avio_rb64(pb); // rc_buffer_aggressivity
  602. codecpar->codec_tag = avio_rb32(pb);
  603. avio_r8(pb); // thread_count
  604. avio_rb32(pb); // coder_type
  605. avio_rb32(pb); // me_cmp
  606. avio_rb32(pb); // me_subpel_quality
  607. avio_rb32(pb); // me_range
  608. avio_rb32(pb); // keyint_min
  609. avio_rb32(pb); // scenechange_threshold
  610. avio_rb32(pb); // b_frame_strategy
  611. avio_rb64(pb); // qcompress
  612. avio_rb64(pb); // qblur
  613. avio_rb32(pb); // max_qdiff
  614. avio_rb32(pb); // refs
  615. break;
  616. case AVMEDIA_TYPE_AUDIO:
  617. codecpar->sample_rate = avio_rb32(pb);
  618. VALIDATE_PARAMETER(sample_rate, "sample rate", codecpar->sample_rate < 0)
  619. codecpar->channels = avio_rl16(pb);
  620. VALIDATE_PARAMETER(channels, "number of channels", codecpar->channels < 0)
  621. codecpar->frame_size = avio_rl16(pb);
  622. VALIDATE_PARAMETER(frame_size, "frame size", codecpar->frame_size < 0)
  623. break;
  624. default:
  625. goto fail;
  626. }
  627. if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  628. int size = avio_rb32(pb);
  629. if (size < 0 || size >= FF_MAX_EXTRADATA_SIZE) {
  630. av_log(s, AV_LOG_ERROR, "Invalid extradata size %d\n", size);
  631. goto fail;
  632. }
  633. codecpar->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
  634. if (!codecpar->extradata)
  635. return AVERROR(ENOMEM);
  636. codecpar->extradata_size = size;
  637. avio_read(pb, codecpar->extradata, size);
  638. }
  639. }
  640. /* get until end of block reached */
  641. while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
  642. avio_r8(pb);
  643. /* init packet demux */
  644. ffm->packet_ptr = ffm->packet;
  645. ffm->packet_end = ffm->packet;
  646. ffm->frame_offset = 0;
  647. ffm->dts = 0;
  648. ffm->read_state = READ_HEADER;
  649. ffm->first_packet = 1;
  650. avcodec_free_context(&dummy_codec);
  651. return 0;
  652. fail:
  653. avcodec_free_context(&dummy_codec);
  654. return -1;
  655. }
  656. /* return < 0 if eof */
  657. static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
  658. {
  659. int size;
  660. FFMContext *ffm = s->priv_data;
  661. int duration, ret;
  662. switch(ffm->read_state) {
  663. case READ_HEADER:
  664. if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
  665. return ret;
  666. ff_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
  667. avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
  668. if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
  669. FRAME_HEADER_SIZE)
  670. return -1;
  671. if (ffm->header[1] & FLAG_DTS)
  672. if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
  673. return -1;
  674. ffm->read_state = READ_DATA;
  675. /* fall through */
  676. case READ_DATA:
  677. size = AV_RB24(ffm->header + 2);
  678. if ((ret = ffm_is_avail_data(s, size)) < 0)
  679. return ret;
  680. duration = AV_RB24(ffm->header + 5);
  681. if (av_new_packet(pkt, size) < 0) {
  682. return AVERROR(ENOMEM);
  683. }
  684. pkt->stream_index = ffm->header[0];
  685. if ((unsigned)pkt->stream_index >= s->nb_streams) {
  686. av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
  687. av_packet_unref(pkt);
  688. ffm->read_state = READ_HEADER;
  689. return -1;
  690. }
  691. pkt->pos = avio_tell(s->pb);
  692. if (ffm->header[1] & FLAG_KEY_FRAME)
  693. pkt->flags |= AV_PKT_FLAG_KEY;
  694. ffm->read_state = READ_HEADER;
  695. if (ffm_read_data(s, pkt->data, size, 0) != size) {
  696. /* bad case: desynchronized packet. we cancel all the packet loading */
  697. av_packet_unref(pkt);
  698. return -1;
  699. }
  700. pkt->pts = AV_RB64(ffm->header+8);
  701. if (ffm->header[1] & FLAG_DTS)
  702. pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
  703. else
  704. pkt->dts = pkt->pts;
  705. pkt->duration = duration;
  706. break;
  707. }
  708. return 0;
  709. }
  710. /* seek to a given time in the file. The file read pointer is
  711. positioned at or before pts. XXX: the following code is quite
  712. approximative */
  713. static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
  714. {
  715. FFMContext *ffm = s->priv_data;
  716. int64_t pos_min, pos_max, pos;
  717. int64_t pts_min, pts_max, pts;
  718. double pos1;
  719. ff_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
  720. /* find the position using linear interpolation (better than
  721. dichotomy in typical cases) */
  722. if (ffm->write_index && ffm->write_index < ffm->file_size) {
  723. if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
  724. pos_min = FFM_PACKET_SIZE;
  725. pos_max = ffm->write_index - FFM_PACKET_SIZE;
  726. } else {
  727. pos_min = ffm->write_index;
  728. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  729. }
  730. } else {
  731. pos_min = FFM_PACKET_SIZE;
  732. pos_max = ffm->file_size - FFM_PACKET_SIZE;
  733. }
  734. while (pos_min <= pos_max) {
  735. pts_min = get_dts(s, pos_min);
  736. pts_max = get_dts(s, pos_max);
  737. if (pts_min > wanted_pts || pts_max <= wanted_pts) {
  738. pos = pts_min > wanted_pts ? pos_min : pos_max;
  739. goto found;
  740. }
  741. /* linear interpolation */
  742. pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
  743. (double)(pts_max - pts_min);
  744. pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
  745. if (pos <= pos_min)
  746. pos = pos_min;
  747. else if (pos >= pos_max)
  748. pos = pos_max;
  749. pts = get_dts(s, pos);
  750. /* check if we are lucky */
  751. if (pts == wanted_pts) {
  752. goto found;
  753. } else if (pts > wanted_pts) {
  754. pos_max = pos - FFM_PACKET_SIZE;
  755. } else {
  756. pos_min = pos + FFM_PACKET_SIZE;
  757. }
  758. }
  759. pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
  760. found:
  761. if (ffm_seek1(s, pos) < 0)
  762. return -1;
  763. /* reset read state */
  764. ffm->read_state = READ_HEADER;
  765. ffm->packet_ptr = ffm->packet;
  766. ffm->packet_end = ffm->packet;
  767. ffm->first_packet = 1;
  768. return 0;
  769. }
  770. static int ffm_probe(AVProbeData *p)
  771. {
  772. if (
  773. p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
  774. (p->buf[3] == '1' || p->buf[3] == '2'))
  775. return AVPROBE_SCORE_MAX + 1;
  776. return 0;
  777. }
  778. static const AVOption options[] = {
  779. {"server_attached", NULL, offsetof(FFMContext, server_attached), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
  780. {"ffm_write_index", NULL, offsetof(FFMContext, write_index), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
  781. {"ffm_file_size", NULL, offsetof(FFMContext, file_size), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
  782. { NULL },
  783. };
  784. static const AVClass ffm_class = {
  785. .class_name = "ffm demuxer",
  786. .item_name = av_default_item_name,
  787. .option = options,
  788. .version = LIBAVUTIL_VERSION_INT,
  789. };
  790. AVInputFormat ff_ffm_demuxer = {
  791. .name = "ffm",
  792. .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
  793. .priv_data_size = sizeof(FFMContext),
  794. .read_probe = ffm_probe,
  795. .read_header = ffm_read_header,
  796. .read_packet = ffm_read_packet,
  797. .read_seek = ffm_seek,
  798. .priv_class = &ffm_class,
  799. };