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.

1992 lines
64KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "config.h"
  27. #include "libavutil/attributes.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/bprint.h"
  31. #include "libavutil/channel_layout.h"
  32. #include "libavutil/crc.h"
  33. #include "libavutil/frame.h"
  34. #include "libavutil/hwcontext.h"
  35. #include "libavutil/internal.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/mem_internal.h"
  38. #include "libavutil/pixdesc.h"
  39. #include "libavutil/imgutils.h"
  40. #include "libavutil/samplefmt.h"
  41. #include "libavutil/dict.h"
  42. #include "libavutil/thread.h"
  43. #include "avcodec.h"
  44. #include "decode.h"
  45. #include "encode.h"
  46. #include "hwconfig.h"
  47. #include "libavutil/opt.h"
  48. #include "mpegvideo.h"
  49. #include "thread.h"
  50. #include "frame_thread_encoder.h"
  51. #include "internal.h"
  52. #include "put_bits.h"
  53. #include "raw.h"
  54. #include "bytestream.h"
  55. #include "version.h"
  56. #include <stdlib.h>
  57. #include <stdarg.h>
  58. #include <stdatomic.h>
  59. #include <limits.h>
  60. #include <float.h>
  61. #if CONFIG_ICONV
  62. # include <iconv.h>
  63. #endif
  64. #include "libavutil/ffversion.h"
  65. const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
  66. static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
  67. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  68. {
  69. uint8_t **p = ptr;
  70. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  71. av_freep(p);
  72. *size = 0;
  73. return;
  74. }
  75. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  76. memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  77. }
  78. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  79. {
  80. uint8_t **p = ptr;
  81. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  82. av_freep(p);
  83. *size = 0;
  84. return;
  85. }
  86. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  87. memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
  88. }
  89. int av_codec_is_encoder(const AVCodec *codec)
  90. {
  91. return codec && (codec->encode_sub || codec->encode2 || codec->receive_packet);
  92. }
  93. int av_codec_is_decoder(const AVCodec *codec)
  94. {
  95. return codec && (codec->decode || codec->receive_frame);
  96. }
  97. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  98. {
  99. int ret = av_image_check_size2(width, height, s->max_pixels, AV_PIX_FMT_NONE, 0, s);
  100. if (ret < 0)
  101. width = height = 0;
  102. s->coded_width = width;
  103. s->coded_height = height;
  104. s->width = AV_CEIL_RSHIFT(width, s->lowres);
  105. s->height = AV_CEIL_RSHIFT(height, s->lowres);
  106. return ret;
  107. }
  108. int ff_set_sar(AVCodecContext *avctx, AVRational sar)
  109. {
  110. int ret = av_image_check_sar(avctx->width, avctx->height, sar);
  111. if (ret < 0) {
  112. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
  113. sar.num, sar.den);
  114. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  115. return ret;
  116. } else {
  117. avctx->sample_aspect_ratio = sar;
  118. }
  119. return 0;
  120. }
  121. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  122. enum AVMatrixEncoding matrix_encoding)
  123. {
  124. AVFrameSideData *side_data;
  125. enum AVMatrixEncoding *data;
  126. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
  127. if (!side_data)
  128. side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
  129. sizeof(enum AVMatrixEncoding));
  130. if (!side_data)
  131. return AVERROR(ENOMEM);
  132. data = (enum AVMatrixEncoding*)side_data->data;
  133. *data = matrix_encoding;
  134. return 0;
  135. }
  136. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  137. int linesize_align[AV_NUM_DATA_POINTERS])
  138. {
  139. int i;
  140. int w_align = 1;
  141. int h_align = 1;
  142. AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
  143. if (desc) {
  144. w_align = 1 << desc->log2_chroma_w;
  145. h_align = 1 << desc->log2_chroma_h;
  146. }
  147. switch (s->pix_fmt) {
  148. case AV_PIX_FMT_YUV420P:
  149. case AV_PIX_FMT_YUYV422:
  150. case AV_PIX_FMT_YVYU422:
  151. case AV_PIX_FMT_UYVY422:
  152. case AV_PIX_FMT_YUV422P:
  153. case AV_PIX_FMT_YUV440P:
  154. case AV_PIX_FMT_YUV444P:
  155. case AV_PIX_FMT_GBRP:
  156. case AV_PIX_FMT_GBRAP:
  157. case AV_PIX_FMT_GRAY8:
  158. case AV_PIX_FMT_GRAY16BE:
  159. case AV_PIX_FMT_GRAY16LE:
  160. case AV_PIX_FMT_YUVJ420P:
  161. case AV_PIX_FMT_YUVJ422P:
  162. case AV_PIX_FMT_YUVJ440P:
  163. case AV_PIX_FMT_YUVJ444P:
  164. case AV_PIX_FMT_YUVA420P:
  165. case AV_PIX_FMT_YUVA422P:
  166. case AV_PIX_FMT_YUVA444P:
  167. case AV_PIX_FMT_YUV420P9LE:
  168. case AV_PIX_FMT_YUV420P9BE:
  169. case AV_PIX_FMT_YUV420P10LE:
  170. case AV_PIX_FMT_YUV420P10BE:
  171. case AV_PIX_FMT_YUV420P12LE:
  172. case AV_PIX_FMT_YUV420P12BE:
  173. case AV_PIX_FMT_YUV420P14LE:
  174. case AV_PIX_FMT_YUV420P14BE:
  175. case AV_PIX_FMT_YUV420P16LE:
  176. case AV_PIX_FMT_YUV420P16BE:
  177. case AV_PIX_FMT_YUVA420P9LE:
  178. case AV_PIX_FMT_YUVA420P9BE:
  179. case AV_PIX_FMT_YUVA420P10LE:
  180. case AV_PIX_FMT_YUVA420P10BE:
  181. case AV_PIX_FMT_YUVA420P16LE:
  182. case AV_PIX_FMT_YUVA420P16BE:
  183. case AV_PIX_FMT_YUV422P9LE:
  184. case AV_PIX_FMT_YUV422P9BE:
  185. case AV_PIX_FMT_YUV422P10LE:
  186. case AV_PIX_FMT_YUV422P10BE:
  187. case AV_PIX_FMT_YUV422P12LE:
  188. case AV_PIX_FMT_YUV422P12BE:
  189. case AV_PIX_FMT_YUV422P14LE:
  190. case AV_PIX_FMT_YUV422P14BE:
  191. case AV_PIX_FMT_YUV422P16LE:
  192. case AV_PIX_FMT_YUV422P16BE:
  193. case AV_PIX_FMT_YUVA422P9LE:
  194. case AV_PIX_FMT_YUVA422P9BE:
  195. case AV_PIX_FMT_YUVA422P10LE:
  196. case AV_PIX_FMT_YUVA422P10BE:
  197. case AV_PIX_FMT_YUVA422P12LE:
  198. case AV_PIX_FMT_YUVA422P12BE:
  199. case AV_PIX_FMT_YUVA422P16LE:
  200. case AV_PIX_FMT_YUVA422P16BE:
  201. case AV_PIX_FMT_YUV440P10LE:
  202. case AV_PIX_FMT_YUV440P10BE:
  203. case AV_PIX_FMT_YUV440P12LE:
  204. case AV_PIX_FMT_YUV440P12BE:
  205. case AV_PIX_FMT_YUV444P9LE:
  206. case AV_PIX_FMT_YUV444P9BE:
  207. case AV_PIX_FMT_YUV444P10LE:
  208. case AV_PIX_FMT_YUV444P10BE:
  209. case AV_PIX_FMT_YUV444P12LE:
  210. case AV_PIX_FMT_YUV444P12BE:
  211. case AV_PIX_FMT_YUV444P14LE:
  212. case AV_PIX_FMT_YUV444P14BE:
  213. case AV_PIX_FMT_YUV444P16LE:
  214. case AV_PIX_FMT_YUV444P16BE:
  215. case AV_PIX_FMT_YUVA444P9LE:
  216. case AV_PIX_FMT_YUVA444P9BE:
  217. case AV_PIX_FMT_YUVA444P10LE:
  218. case AV_PIX_FMT_YUVA444P10BE:
  219. case AV_PIX_FMT_YUVA444P12LE:
  220. case AV_PIX_FMT_YUVA444P12BE:
  221. case AV_PIX_FMT_YUVA444P16LE:
  222. case AV_PIX_FMT_YUVA444P16BE:
  223. case AV_PIX_FMT_GBRP9LE:
  224. case AV_PIX_FMT_GBRP9BE:
  225. case AV_PIX_FMT_GBRP10LE:
  226. case AV_PIX_FMT_GBRP10BE:
  227. case AV_PIX_FMT_GBRP12LE:
  228. case AV_PIX_FMT_GBRP12BE:
  229. case AV_PIX_FMT_GBRP14LE:
  230. case AV_PIX_FMT_GBRP14BE:
  231. case AV_PIX_FMT_GBRP16LE:
  232. case AV_PIX_FMT_GBRP16BE:
  233. case AV_PIX_FMT_GBRAP12LE:
  234. case AV_PIX_FMT_GBRAP12BE:
  235. case AV_PIX_FMT_GBRAP16LE:
  236. case AV_PIX_FMT_GBRAP16BE:
  237. w_align = 16; //FIXME assume 16 pixel per macroblock
  238. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  239. break;
  240. case AV_PIX_FMT_YUV411P:
  241. case AV_PIX_FMT_YUVJ411P:
  242. case AV_PIX_FMT_UYYVYY411:
  243. w_align = 32;
  244. h_align = 16 * 2;
  245. break;
  246. case AV_PIX_FMT_YUV410P:
  247. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  248. w_align = 64;
  249. h_align = 64;
  250. }
  251. break;
  252. case AV_PIX_FMT_RGB555:
  253. if (s->codec_id == AV_CODEC_ID_RPZA) {
  254. w_align = 4;
  255. h_align = 4;
  256. }
  257. if (s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
  258. w_align = 8;
  259. h_align = 8;
  260. }
  261. break;
  262. case AV_PIX_FMT_PAL8:
  263. case AV_PIX_FMT_BGR8:
  264. case AV_PIX_FMT_RGB8:
  265. if (s->codec_id == AV_CODEC_ID_SMC ||
  266. s->codec_id == AV_CODEC_ID_CINEPAK) {
  267. w_align = 4;
  268. h_align = 4;
  269. }
  270. if (s->codec_id == AV_CODEC_ID_JV ||
  271. s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
  272. w_align = 8;
  273. h_align = 8;
  274. }
  275. break;
  276. case AV_PIX_FMT_BGR24:
  277. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  278. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  279. w_align = 4;
  280. h_align = 4;
  281. }
  282. break;
  283. case AV_PIX_FMT_RGB24:
  284. if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  285. w_align = 4;
  286. h_align = 4;
  287. }
  288. break;
  289. default:
  290. break;
  291. }
  292. if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
  293. w_align = FFMAX(w_align, 8);
  294. }
  295. *width = FFALIGN(*width, w_align);
  296. *height = FFALIGN(*height, h_align);
  297. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres ||
  298. s->codec_id == AV_CODEC_ID_VP5 || s->codec_id == AV_CODEC_ID_VP6 ||
  299. s->codec_id == AV_CODEC_ID_VP6F || s->codec_id == AV_CODEC_ID_VP6A
  300. ) {
  301. // some of the optimized chroma MC reads one line too much
  302. // which is also done in mpeg decoders with lowres > 0
  303. *height += 2;
  304. // H.264 uses edge emulation for out of frame motion vectors, for this
  305. // it requires a temporary area large enough to hold a 21x21 block,
  306. // increasing witdth ensure that the temporary area is large enough,
  307. // the next rounded up width is 32
  308. *width = FFMAX(*width, 32);
  309. }
  310. for (i = 0; i < 4; i++)
  311. linesize_align[i] = STRIDE_ALIGN;
  312. }
  313. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  314. {
  315. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  316. int chroma_shift = desc->log2_chroma_w;
  317. int linesize_align[AV_NUM_DATA_POINTERS];
  318. int align;
  319. avcodec_align_dimensions2(s, width, height, linesize_align);
  320. align = FFMAX(linesize_align[0], linesize_align[3]);
  321. linesize_align[1] <<= chroma_shift;
  322. linesize_align[2] <<= chroma_shift;
  323. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  324. *width = FFALIGN(*width, align);
  325. }
  326. int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
  327. {
  328. if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
  329. return AVERROR(EINVAL);
  330. pos--;
  331. *xpos = (pos&1) * 128;
  332. *ypos = ((pos>>1)^(pos<4)) * 128;
  333. return 0;
  334. }
  335. enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
  336. {
  337. int pos, xout, yout;
  338. for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
  339. if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
  340. return pos;
  341. }
  342. return AVCHROMA_LOC_UNSPECIFIED;
  343. }
  344. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  345. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  346. int buf_size, int align)
  347. {
  348. int ch, planar, needed_size, ret = 0;
  349. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  350. frame->nb_samples, sample_fmt,
  351. align);
  352. if (buf_size < needed_size)
  353. return AVERROR(EINVAL);
  354. planar = av_sample_fmt_is_planar(sample_fmt);
  355. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  356. if (!(frame->extended_data = av_mallocz_array(nb_channels,
  357. sizeof(*frame->extended_data))))
  358. return AVERROR(ENOMEM);
  359. } else {
  360. frame->extended_data = frame->data;
  361. }
  362. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  363. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  364. sample_fmt, align)) < 0) {
  365. if (frame->extended_data != frame->data)
  366. av_freep(&frame->extended_data);
  367. return ret;
  368. }
  369. if (frame->extended_data != frame->data) {
  370. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  371. frame->data[ch] = frame->extended_data[ch];
  372. }
  373. return ret;
  374. }
  375. void ff_color_frame(AVFrame *frame, const int c[4])
  376. {
  377. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  378. int p, y;
  379. av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
  380. for (p = 0; p<desc->nb_components; p++) {
  381. uint8_t *dst = frame->data[p];
  382. int is_chroma = p == 1 || p == 2;
  383. int bytes = is_chroma ? AV_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
  384. int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
  385. if (desc->comp[0].depth >= 9) {
  386. ((uint16_t*)dst)[0] = c[p];
  387. av_memcpy_backptr(dst + 2, 2, bytes - 2);
  388. dst += frame->linesize[p];
  389. for (y = 1; y < height; y++) {
  390. memcpy(dst, frame->data[p], 2*bytes);
  391. dst += frame->linesize[p];
  392. }
  393. } else {
  394. for (y = 0; y < height; y++) {
  395. memset(dst, c[p], bytes);
  396. dst += frame->linesize[p];
  397. }
  398. }
  399. }
  400. }
  401. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  402. {
  403. int i;
  404. for (i = 0; i < count; i++) {
  405. int r = func(c, (char *)arg + i * size);
  406. if (ret)
  407. ret[i] = r;
  408. }
  409. emms_c();
  410. return 0;
  411. }
  412. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  413. {
  414. int i;
  415. for (i = 0; i < count; i++) {
  416. int r = func(c, arg, i, 0);
  417. if (ret)
  418. ret[i] = r;
  419. }
  420. emms_c();
  421. return 0;
  422. }
  423. enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
  424. unsigned int fourcc)
  425. {
  426. while (tags->pix_fmt >= 0) {
  427. if (tags->fourcc == fourcc)
  428. return tags->pix_fmt;
  429. tags++;
  430. }
  431. return AV_PIX_FMT_NONE;
  432. }
  433. #if FF_API_CODEC_GET_SET
  434. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  435. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  436. MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
  437. MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
  438. MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
  439. unsigned av_codec_get_codec_properties(const AVCodecContext *codec)
  440. {
  441. return codec->properties;
  442. }
  443. int av_codec_get_max_lowres(const AVCodec *codec)
  444. {
  445. return codec->max_lowres;
  446. }
  447. #endif
  448. int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec){
  449. return !!(codec->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM);
  450. }
  451. static int64_t get_bit_rate(AVCodecContext *ctx)
  452. {
  453. int64_t bit_rate;
  454. int bits_per_sample;
  455. switch (ctx->codec_type) {
  456. case AVMEDIA_TYPE_VIDEO:
  457. case AVMEDIA_TYPE_DATA:
  458. case AVMEDIA_TYPE_SUBTITLE:
  459. case AVMEDIA_TYPE_ATTACHMENT:
  460. bit_rate = ctx->bit_rate;
  461. break;
  462. case AVMEDIA_TYPE_AUDIO:
  463. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  464. if (bits_per_sample) {
  465. bit_rate = ctx->sample_rate * (int64_t)ctx->channels;
  466. if (bit_rate > INT64_MAX / bits_per_sample) {
  467. bit_rate = 0;
  468. } else
  469. bit_rate *= bits_per_sample;
  470. } else
  471. bit_rate = ctx->bit_rate;
  472. break;
  473. default:
  474. bit_rate = 0;
  475. break;
  476. }
  477. return bit_rate;
  478. }
  479. static void lock_avcodec(const AVCodec *codec)
  480. {
  481. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
  482. ff_mutex_lock(&codec_mutex);
  483. }
  484. static void unlock_avcodec(const AVCodec *codec)
  485. {
  486. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
  487. ff_mutex_unlock(&codec_mutex);
  488. }
  489. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  490. {
  491. int ret = 0;
  492. int codec_init_ok = 0;
  493. AVDictionary *tmp = NULL;
  494. AVCodecInternal *avci;
  495. if (avcodec_is_open(avctx))
  496. return 0;
  497. if (!codec && !avctx->codec) {
  498. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  499. return AVERROR(EINVAL);
  500. }
  501. if (codec && avctx->codec && codec != avctx->codec) {
  502. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  503. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  504. return AVERROR(EINVAL);
  505. }
  506. if (!codec)
  507. codec = avctx->codec;
  508. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  509. return AVERROR(EINVAL);
  510. if (options)
  511. av_dict_copy(&tmp, *options, 0);
  512. lock_avcodec(codec);
  513. avci = av_mallocz(sizeof(*avci));
  514. if (!avci) {
  515. ret = AVERROR(ENOMEM);
  516. goto end;
  517. }
  518. avctx->internal = avci;
  519. #if FF_API_OLD_ENCDEC
  520. avci->to_free = av_frame_alloc();
  521. avci->compat_decode_frame = av_frame_alloc();
  522. avci->compat_encode_packet = av_packet_alloc();
  523. if (!avci->to_free || !avci->compat_decode_frame || !avci->compat_encode_packet) {
  524. ret = AVERROR(ENOMEM);
  525. goto free_and_end;
  526. }
  527. #endif
  528. avci->buffer_frame = av_frame_alloc();
  529. avci->buffer_pkt = av_packet_alloc();
  530. avci->es.in_frame = av_frame_alloc();
  531. avci->ds.in_pkt = av_packet_alloc();
  532. avci->last_pkt_props = av_packet_alloc();
  533. avci->pkt_props = av_fifo_alloc(sizeof(*avci->last_pkt_props));
  534. if (!avci->buffer_frame || !avci->buffer_pkt ||
  535. !avci->es.in_frame || !avci->ds.in_pkt ||
  536. !avci->last_pkt_props || !avci->pkt_props) {
  537. ret = AVERROR(ENOMEM);
  538. goto free_and_end;
  539. }
  540. avci->skip_samples_multiplier = 1;
  541. if (codec->priv_data_size > 0) {
  542. if (!avctx->priv_data) {
  543. avctx->priv_data = av_mallocz(codec->priv_data_size);
  544. if (!avctx->priv_data) {
  545. ret = AVERROR(ENOMEM);
  546. goto free_and_end;
  547. }
  548. if (codec->priv_class) {
  549. *(const AVClass **)avctx->priv_data = codec->priv_class;
  550. av_opt_set_defaults(avctx->priv_data);
  551. }
  552. }
  553. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  554. goto free_and_end;
  555. } else {
  556. avctx->priv_data = NULL;
  557. }
  558. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  559. goto free_and_end;
  560. if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
  561. av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
  562. ret = AVERROR(EINVAL);
  563. goto free_and_end;
  564. }
  565. // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
  566. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  567. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
  568. if (avctx->coded_width && avctx->coded_height)
  569. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  570. else if (avctx->width && avctx->height)
  571. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  572. if (ret < 0)
  573. goto free_and_end;
  574. }
  575. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  576. && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
  577. || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
  578. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  579. ff_set_dimensions(avctx, 0, 0);
  580. }
  581. if (avctx->width > 0 && avctx->height > 0) {
  582. if (av_image_check_sar(avctx->width, avctx->height,
  583. avctx->sample_aspect_ratio) < 0) {
  584. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  585. avctx->sample_aspect_ratio.num,
  586. avctx->sample_aspect_ratio.den);
  587. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  588. }
  589. }
  590. if (avctx->channels > FF_SANE_NB_CHANNELS || avctx->channels < 0) {
  591. av_log(avctx, AV_LOG_ERROR, "Too many or invalid channels: %d\n", avctx->channels);
  592. ret = AVERROR(EINVAL);
  593. goto free_and_end;
  594. }
  595. if (av_codec_is_decoder(codec) &&
  596. codec->type == AVMEDIA_TYPE_AUDIO &&
  597. !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF) &&
  598. avctx->channels == 0) {
  599. av_log(avctx, AV_LOG_ERROR, "Decoder requires channel count but channels not set\n");
  600. ret = AVERROR(EINVAL);
  601. goto free_and_end;
  602. }
  603. if (avctx->sample_rate < 0) {
  604. av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
  605. ret = AVERROR(EINVAL);
  606. goto free_and_end;
  607. }
  608. if (avctx->block_align < 0) {
  609. av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
  610. ret = AVERROR(EINVAL);
  611. goto free_and_end;
  612. }
  613. avctx->codec = codec;
  614. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  615. avctx->codec_id == AV_CODEC_ID_NONE) {
  616. avctx->codec_type = codec->type;
  617. avctx->codec_id = codec->id;
  618. }
  619. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  620. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  621. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  622. ret = AVERROR(EINVAL);
  623. goto free_and_end;
  624. }
  625. avctx->frame_number = 0;
  626. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  627. if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  628. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  629. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  630. const AVCodec *codec2;
  631. av_log(avctx, AV_LOG_ERROR,
  632. "The %s '%s' is experimental but experimental codecs are not enabled, "
  633. "add '-strict %d' if you want to use it.\n",
  634. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  635. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  636. if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
  637. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  638. codec_string, codec2->name);
  639. ret = AVERROR_EXPERIMENTAL;
  640. goto free_and_end;
  641. }
  642. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  643. (!avctx->time_base.num || !avctx->time_base.den)) {
  644. avctx->time_base.num = 1;
  645. avctx->time_base.den = avctx->sample_rate;
  646. }
  647. if (!HAVE_THREADS)
  648. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  649. if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
  650. unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
  651. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  652. lock_avcodec(codec);
  653. if (ret < 0)
  654. goto free_and_end;
  655. }
  656. if (HAVE_THREADS
  657. && !(avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  658. ret = ff_thread_init(avctx);
  659. if (ret < 0) {
  660. goto free_and_end;
  661. }
  662. }
  663. if (!HAVE_THREADS && !(codec->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
  664. avctx->thread_count = 1;
  665. if (av_codec_is_encoder(avctx->codec))
  666. ret = ff_encode_preinit(avctx);
  667. else
  668. ret = ff_decode_preinit(avctx);
  669. if (ret < 0)
  670. goto free_and_end;
  671. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  672. || avci->frame_thread_encoder)) {
  673. ret = avctx->codec->init(avctx);
  674. if (ret < 0) {
  675. codec_init_ok = -1;
  676. goto free_and_end;
  677. }
  678. codec_init_ok = 1;
  679. }
  680. ret=0;
  681. if (av_codec_is_decoder(avctx->codec)) {
  682. if (!avctx->bit_rate)
  683. avctx->bit_rate = get_bit_rate(avctx);
  684. /* validate channel layout from the decoder */
  685. if (avctx->channel_layout) {
  686. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  687. if (!avctx->channels)
  688. avctx->channels = channels;
  689. else if (channels != avctx->channels) {
  690. char buf[512];
  691. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  692. av_log(avctx, AV_LOG_WARNING,
  693. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  694. "ignoring specified channel layout\n",
  695. buf, channels, avctx->channels);
  696. avctx->channel_layout = 0;
  697. }
  698. }
  699. if (avctx->channels && avctx->channels < 0 ||
  700. avctx->channels > FF_SANE_NB_CHANNELS) {
  701. ret = AVERROR(EINVAL);
  702. goto free_and_end;
  703. }
  704. if (avctx->bits_per_coded_sample < 0) {
  705. ret = AVERROR(EINVAL);
  706. goto free_and_end;
  707. }
  708. if (avctx->sub_charenc) {
  709. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  710. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  711. "supported with subtitles codecs\n");
  712. ret = AVERROR(EINVAL);
  713. goto free_and_end;
  714. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  715. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  716. "subtitles character encoding will be ignored\n",
  717. avctx->codec_descriptor->name);
  718. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  719. } else {
  720. /* input character encoding is set for a text based subtitle
  721. * codec at this point */
  722. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  723. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  724. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  725. #if CONFIG_ICONV
  726. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  727. if (cd == (iconv_t)-1) {
  728. ret = AVERROR(errno);
  729. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  730. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  731. goto free_and_end;
  732. }
  733. iconv_close(cd);
  734. #else
  735. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  736. "conversion needs a libavcodec built with iconv support "
  737. "for this codec\n");
  738. ret = AVERROR(ENOSYS);
  739. goto free_and_end;
  740. #endif
  741. }
  742. }
  743. }
  744. #if FF_API_AVCTX_TIMEBASE
  745. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  746. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  747. #endif
  748. }
  749. if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
  750. av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
  751. }
  752. end:
  753. unlock_avcodec(codec);
  754. if (options) {
  755. av_dict_free(options);
  756. *options = tmp;
  757. }
  758. return ret;
  759. free_and_end:
  760. if (avctx->codec && avctx->codec->close &&
  761. (codec_init_ok > 0 || (codec_init_ok < 0 &&
  762. avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)))
  763. avctx->codec->close(avctx);
  764. if (HAVE_THREADS && avci->thread_ctx)
  765. ff_thread_free(avctx);
  766. if (codec->priv_class && avctx->priv_data)
  767. av_opt_free(avctx->priv_data);
  768. av_opt_free(avctx);
  769. if (av_codec_is_encoder(avctx->codec)) {
  770. #if FF_API_CODED_FRAME
  771. FF_DISABLE_DEPRECATION_WARNINGS
  772. av_frame_free(&avctx->coded_frame);
  773. FF_ENABLE_DEPRECATION_WARNINGS
  774. #endif
  775. av_freep(&avctx->extradata);
  776. avctx->extradata_size = 0;
  777. }
  778. av_dict_free(&tmp);
  779. av_freep(&avctx->priv_data);
  780. av_freep(&avctx->subtitle_header);
  781. #if FF_API_OLD_ENCDEC
  782. av_frame_free(&avci->to_free);
  783. av_frame_free(&avci->compat_decode_frame);
  784. av_packet_free(&avci->compat_encode_packet);
  785. #endif
  786. av_frame_free(&avci->buffer_frame);
  787. av_packet_free(&avci->buffer_pkt);
  788. av_packet_free(&avci->last_pkt_props);
  789. av_fifo_freep(&avci->pkt_props);
  790. av_packet_free(&avci->ds.in_pkt);
  791. av_frame_free(&avci->es.in_frame);
  792. av_bsf_free(&avci->bsf);
  793. av_buffer_unref(&avci->pool);
  794. av_freep(&avci);
  795. avctx->internal = NULL;
  796. avctx->codec = NULL;
  797. goto end;
  798. }
  799. void avcodec_flush_buffers(AVCodecContext *avctx)
  800. {
  801. AVCodecInternal *avci = avctx->internal;
  802. if (av_codec_is_encoder(avctx->codec)) {
  803. int caps = avctx->codec->capabilities;
  804. if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
  805. // Only encoders that explicitly declare support for it can be
  806. // flushed. Otherwise, this is a no-op.
  807. av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
  808. "that doesn't support it\n");
  809. return;
  810. }
  811. // We haven't implemented flushing for frame-threaded encoders.
  812. av_assert0(!(caps & AV_CODEC_CAP_FRAME_THREADS));
  813. }
  814. avci->draining = 0;
  815. avci->draining_done = 0;
  816. avci->nb_draining_errors = 0;
  817. av_frame_unref(avci->buffer_frame);
  818. #if FF_API_OLD_ENCDEC
  819. av_frame_unref(avci->compat_decode_frame);
  820. av_packet_unref(avci->compat_encode_packet);
  821. #endif
  822. av_packet_unref(avci->buffer_pkt);
  823. av_packet_unref(avci->last_pkt_props);
  824. while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
  825. av_fifo_generic_read(avci->pkt_props,
  826. avci->last_pkt_props, sizeof(*avci->last_pkt_props),
  827. NULL);
  828. av_packet_unref(avci->last_pkt_props);
  829. }
  830. av_fifo_reset(avci->pkt_props);
  831. av_frame_unref(avci->es.in_frame);
  832. av_packet_unref(avci->ds.in_pkt);
  833. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  834. ff_thread_flush(avctx);
  835. else if (avctx->codec->flush)
  836. avctx->codec->flush(avctx);
  837. avctx->pts_correction_last_pts =
  838. avctx->pts_correction_last_dts = INT64_MIN;
  839. if (av_codec_is_decoder(avctx->codec))
  840. av_bsf_flush(avci->bsf);
  841. #if FF_API_OLD_ENCDEC
  842. FF_DISABLE_DEPRECATION_WARNINGS
  843. if (!avctx->refcounted_frames)
  844. av_frame_unref(avci->to_free);
  845. FF_ENABLE_DEPRECATION_WARNINGS
  846. #endif
  847. }
  848. void avsubtitle_free(AVSubtitle *sub)
  849. {
  850. int i;
  851. for (i = 0; i < sub->num_rects; i++) {
  852. av_freep(&sub->rects[i]->data[0]);
  853. av_freep(&sub->rects[i]->data[1]);
  854. av_freep(&sub->rects[i]->data[2]);
  855. av_freep(&sub->rects[i]->data[3]);
  856. av_freep(&sub->rects[i]->text);
  857. av_freep(&sub->rects[i]->ass);
  858. av_freep(&sub->rects[i]);
  859. }
  860. av_freep(&sub->rects);
  861. memset(sub, 0, sizeof(*sub));
  862. }
  863. av_cold int avcodec_close(AVCodecContext *avctx)
  864. {
  865. int i;
  866. if (!avctx)
  867. return 0;
  868. if (avcodec_is_open(avctx)) {
  869. if (CONFIG_FRAME_THREAD_ENCODER &&
  870. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  871. ff_frame_thread_encoder_free(avctx);
  872. }
  873. if (HAVE_THREADS && avctx->internal->thread_ctx)
  874. ff_thread_free(avctx);
  875. if (avctx->codec && avctx->codec->close)
  876. avctx->codec->close(avctx);
  877. avctx->internal->byte_buffer_size = 0;
  878. av_freep(&avctx->internal->byte_buffer);
  879. #if FF_API_OLD_ENCDEC
  880. av_frame_free(&avctx->internal->to_free);
  881. av_frame_free(&avctx->internal->compat_decode_frame);
  882. av_packet_free(&avctx->internal->compat_encode_packet);
  883. #endif
  884. av_frame_free(&avctx->internal->buffer_frame);
  885. av_packet_free(&avctx->internal->buffer_pkt);
  886. av_packet_unref(avctx->internal->last_pkt_props);
  887. while (av_fifo_size(avctx->internal->pkt_props) >=
  888. sizeof(*avctx->internal->last_pkt_props)) {
  889. av_fifo_generic_read(avctx->internal->pkt_props,
  890. avctx->internal->last_pkt_props,
  891. sizeof(*avctx->internal->last_pkt_props),
  892. NULL);
  893. av_packet_unref(avctx->internal->last_pkt_props);
  894. }
  895. av_packet_free(&avctx->internal->last_pkt_props);
  896. av_fifo_freep(&avctx->internal->pkt_props);
  897. av_packet_free(&avctx->internal->ds.in_pkt);
  898. av_frame_free(&avctx->internal->es.in_frame);
  899. av_buffer_unref(&avctx->internal->pool);
  900. if (avctx->hwaccel && avctx->hwaccel->uninit)
  901. avctx->hwaccel->uninit(avctx);
  902. av_freep(&avctx->internal->hwaccel_priv_data);
  903. av_bsf_free(&avctx->internal->bsf);
  904. av_freep(&avctx->internal);
  905. }
  906. for (i = 0; i < avctx->nb_coded_side_data; i++)
  907. av_freep(&avctx->coded_side_data[i].data);
  908. av_freep(&avctx->coded_side_data);
  909. avctx->nb_coded_side_data = 0;
  910. av_buffer_unref(&avctx->hw_frames_ctx);
  911. av_buffer_unref(&avctx->hw_device_ctx);
  912. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  913. av_opt_free(avctx->priv_data);
  914. av_opt_free(avctx);
  915. av_freep(&avctx->priv_data);
  916. if (av_codec_is_encoder(avctx->codec)) {
  917. av_freep(&avctx->extradata);
  918. #if FF_API_CODED_FRAME
  919. FF_DISABLE_DEPRECATION_WARNINGS
  920. av_frame_free(&avctx->coded_frame);
  921. FF_ENABLE_DEPRECATION_WARNINGS
  922. #endif
  923. }
  924. avctx->codec = NULL;
  925. avctx->active_thread_type = 0;
  926. return 0;
  927. }
  928. const char *avcodec_get_name(enum AVCodecID id)
  929. {
  930. const AVCodecDescriptor *cd;
  931. const AVCodec *codec;
  932. if (id == AV_CODEC_ID_NONE)
  933. return "none";
  934. cd = avcodec_descriptor_get(id);
  935. if (cd)
  936. return cd->name;
  937. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  938. codec = avcodec_find_decoder(id);
  939. if (codec)
  940. return codec->name;
  941. codec = avcodec_find_encoder(id);
  942. if (codec)
  943. return codec->name;
  944. return "unknown_codec";
  945. }
  946. #if FF_API_TAG_STRING
  947. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  948. {
  949. int i, len, ret = 0;
  950. #define TAG_PRINT(x) \
  951. (((x) >= '0' && (x) <= '9') || \
  952. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  953. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  954. for (i = 0; i < 4; i++) {
  955. len = snprintf(buf, buf_size,
  956. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  957. buf += len;
  958. buf_size = buf_size > len ? buf_size - len : 0;
  959. ret += len;
  960. codec_tag >>= 8;
  961. }
  962. return ret;
  963. }
  964. #endif
  965. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  966. {
  967. const char *codec_type;
  968. const char *codec_name;
  969. const char *profile = NULL;
  970. int64_t bitrate;
  971. int new_line = 0;
  972. AVRational display_aspect_ratio;
  973. const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
  974. if (!buf || buf_size <= 0)
  975. return;
  976. codec_type = av_get_media_type_string(enc->codec_type);
  977. codec_name = avcodec_get_name(enc->codec_id);
  978. profile = avcodec_profile_name(enc->codec_id, enc->profile);
  979. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  980. codec_name);
  981. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  982. if (enc->codec && strcmp(enc->codec->name, codec_name))
  983. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  984. if (profile)
  985. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  986. if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
  987. && av_log_get_level() >= AV_LOG_VERBOSE
  988. && enc->refs)
  989. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  990. ", %d reference frame%s",
  991. enc->refs, enc->refs > 1 ? "s" : "");
  992. if (enc->codec_tag)
  993. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s / 0x%04X)",
  994. av_fourcc2str(enc->codec_tag), enc->codec_tag);
  995. switch (enc->codec_type) {
  996. case AVMEDIA_TYPE_VIDEO:
  997. {
  998. char detail[256] = "(";
  999. av_strlcat(buf, separator, buf_size);
  1000. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1001. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  1002. av_get_pix_fmt_name(enc->pix_fmt));
  1003. if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
  1004. enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
  1005. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  1006. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  1007. av_strlcatf(detail, sizeof(detail), "%s, ",
  1008. av_color_range_name(enc->color_range));
  1009. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  1010. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  1011. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  1012. if (enc->colorspace != (int)enc->color_primaries ||
  1013. enc->colorspace != (int)enc->color_trc) {
  1014. new_line = 1;
  1015. av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
  1016. av_color_space_name(enc->colorspace),
  1017. av_color_primaries_name(enc->color_primaries),
  1018. av_color_transfer_name(enc->color_trc));
  1019. } else
  1020. av_strlcatf(detail, sizeof(detail), "%s, ",
  1021. av_get_colorspace_name(enc->colorspace));
  1022. }
  1023. if (enc->field_order != AV_FIELD_UNKNOWN) {
  1024. const char *field_order = "progressive";
  1025. if (enc->field_order == AV_FIELD_TT)
  1026. field_order = "top first";
  1027. else if (enc->field_order == AV_FIELD_BB)
  1028. field_order = "bottom first";
  1029. else if (enc->field_order == AV_FIELD_TB)
  1030. field_order = "top coded first (swapped)";
  1031. else if (enc->field_order == AV_FIELD_BT)
  1032. field_order = "bottom coded first (swapped)";
  1033. av_strlcatf(detail, sizeof(detail), "%s, ", field_order);
  1034. }
  1035. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  1036. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  1037. av_strlcatf(detail, sizeof(detail), "%s, ",
  1038. av_chroma_location_name(enc->chroma_sample_location));
  1039. if (strlen(detail) > 1) {
  1040. detail[strlen(detail) - 2] = 0;
  1041. av_strlcatf(buf, buf_size, "%s)", detail);
  1042. }
  1043. }
  1044. if (enc->width) {
  1045. av_strlcat(buf, new_line ? separator : ", ", buf_size);
  1046. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1047. "%dx%d",
  1048. enc->width, enc->height);
  1049. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  1050. (enc->width != enc->coded_width ||
  1051. enc->height != enc->coded_height))
  1052. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1053. " (%dx%d)", enc->coded_width, enc->coded_height);
  1054. if (enc->sample_aspect_ratio.num) {
  1055. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1056. enc->width * (int64_t)enc->sample_aspect_ratio.num,
  1057. enc->height * (int64_t)enc->sample_aspect_ratio.den,
  1058. 1024 * 1024);
  1059. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1060. " [SAR %d:%d DAR %d:%d]",
  1061. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1062. display_aspect_ratio.num, display_aspect_ratio.den);
  1063. }
  1064. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1065. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1066. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1067. ", %d/%d",
  1068. enc->time_base.num / g, enc->time_base.den / g);
  1069. }
  1070. }
  1071. if (encode) {
  1072. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1073. ", q=%d-%d", enc->qmin, enc->qmax);
  1074. } else {
  1075. if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
  1076. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1077. ", Closed Captions");
  1078. if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
  1079. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1080. ", lossless");
  1081. }
  1082. break;
  1083. case AVMEDIA_TYPE_AUDIO:
  1084. av_strlcat(buf, separator, buf_size);
  1085. if (enc->sample_rate) {
  1086. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1087. "%d Hz, ", enc->sample_rate);
  1088. }
  1089. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1090. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1091. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1092. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1093. }
  1094. if ( enc->bits_per_raw_sample > 0
  1095. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  1096. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1097. " (%d bit)", enc->bits_per_raw_sample);
  1098. if (av_log_get_level() >= AV_LOG_VERBOSE) {
  1099. if (enc->initial_padding)
  1100. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1101. ", delay %d", enc->initial_padding);
  1102. if (enc->trailing_padding)
  1103. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1104. ", padding %d", enc->trailing_padding);
  1105. }
  1106. break;
  1107. case AVMEDIA_TYPE_DATA:
  1108. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1109. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1110. if (g)
  1111. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1112. ", %d/%d",
  1113. enc->time_base.num / g, enc->time_base.den / g);
  1114. }
  1115. break;
  1116. case AVMEDIA_TYPE_SUBTITLE:
  1117. if (enc->width)
  1118. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1119. ", %dx%d", enc->width, enc->height);
  1120. break;
  1121. default:
  1122. return;
  1123. }
  1124. if (encode) {
  1125. if (enc->flags & AV_CODEC_FLAG_PASS1)
  1126. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1127. ", pass 1");
  1128. if (enc->flags & AV_CODEC_FLAG_PASS2)
  1129. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1130. ", pass 2");
  1131. }
  1132. bitrate = get_bit_rate(enc);
  1133. if (bitrate != 0) {
  1134. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1135. ", %"PRId64" kb/s", bitrate / 1000);
  1136. } else if (enc->rc_max_rate > 0) {
  1137. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1138. ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
  1139. }
  1140. }
  1141. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1142. {
  1143. const AVProfile *p;
  1144. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1145. return NULL;
  1146. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1147. if (p->profile == profile)
  1148. return p->name;
  1149. return NULL;
  1150. }
  1151. const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
  1152. {
  1153. const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
  1154. const AVProfile *p;
  1155. if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
  1156. return NULL;
  1157. for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1158. if (p->profile == profile)
  1159. return p->name;
  1160. return NULL;
  1161. }
  1162. unsigned avcodec_version(void)
  1163. {
  1164. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  1165. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  1166. av_assert0(AV_CODEC_ID_SRT==94216);
  1167. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1168. return LIBAVCODEC_VERSION_INT;
  1169. }
  1170. const char *avcodec_configuration(void)
  1171. {
  1172. return FFMPEG_CONFIGURATION;
  1173. }
  1174. const char *avcodec_license(void)
  1175. {
  1176. #define LICENSE_PREFIX "libavcodec license: "
  1177. return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
  1178. }
  1179. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1180. {
  1181. switch (codec_id) {
  1182. case AV_CODEC_ID_8SVX_EXP:
  1183. case AV_CODEC_ID_8SVX_FIB:
  1184. case AV_CODEC_ID_ADPCM_ARGO:
  1185. case AV_CODEC_ID_ADPCM_CT:
  1186. case AV_CODEC_ID_ADPCM_IMA_ALP:
  1187. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1188. case AV_CODEC_ID_ADPCM_IMA_APC:
  1189. case AV_CODEC_ID_ADPCM_IMA_APM:
  1190. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1191. case AV_CODEC_ID_ADPCM_IMA_OKI:
  1192. case AV_CODEC_ID_ADPCM_IMA_WS:
  1193. case AV_CODEC_ID_ADPCM_IMA_SSI:
  1194. case AV_CODEC_ID_ADPCM_G722:
  1195. case AV_CODEC_ID_ADPCM_YAMAHA:
  1196. case AV_CODEC_ID_ADPCM_AICA:
  1197. return 4;
  1198. case AV_CODEC_ID_DSD_LSBF:
  1199. case AV_CODEC_ID_DSD_MSBF:
  1200. case AV_CODEC_ID_DSD_LSBF_PLANAR:
  1201. case AV_CODEC_ID_DSD_MSBF_PLANAR:
  1202. case AV_CODEC_ID_PCM_ALAW:
  1203. case AV_CODEC_ID_PCM_MULAW:
  1204. case AV_CODEC_ID_PCM_VIDC:
  1205. case AV_CODEC_ID_PCM_S8:
  1206. case AV_CODEC_ID_PCM_S8_PLANAR:
  1207. case AV_CODEC_ID_PCM_SGA:
  1208. case AV_CODEC_ID_PCM_U8:
  1209. case AV_CODEC_ID_SDX2_DPCM:
  1210. case AV_CODEC_ID_DERF_DPCM:
  1211. return 8;
  1212. case AV_CODEC_ID_PCM_S16BE:
  1213. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  1214. case AV_CODEC_ID_PCM_S16LE:
  1215. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1216. case AV_CODEC_ID_PCM_U16BE:
  1217. case AV_CODEC_ID_PCM_U16LE:
  1218. return 16;
  1219. case AV_CODEC_ID_PCM_S24DAUD:
  1220. case AV_CODEC_ID_PCM_S24BE:
  1221. case AV_CODEC_ID_PCM_S24LE:
  1222. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  1223. case AV_CODEC_ID_PCM_U24BE:
  1224. case AV_CODEC_ID_PCM_U24LE:
  1225. return 24;
  1226. case AV_CODEC_ID_PCM_S32BE:
  1227. case AV_CODEC_ID_PCM_S32LE:
  1228. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  1229. case AV_CODEC_ID_PCM_U32BE:
  1230. case AV_CODEC_ID_PCM_U32LE:
  1231. case AV_CODEC_ID_PCM_F32BE:
  1232. case AV_CODEC_ID_PCM_F32LE:
  1233. case AV_CODEC_ID_PCM_F24LE:
  1234. case AV_CODEC_ID_PCM_F16LE:
  1235. return 32;
  1236. case AV_CODEC_ID_PCM_F64BE:
  1237. case AV_CODEC_ID_PCM_F64LE:
  1238. case AV_CODEC_ID_PCM_S64BE:
  1239. case AV_CODEC_ID_PCM_S64LE:
  1240. return 64;
  1241. default:
  1242. return 0;
  1243. }
  1244. }
  1245. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1246. {
  1247. static const enum AVCodecID map[][2] = {
  1248. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  1249. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  1250. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  1251. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  1252. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  1253. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  1254. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  1255. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  1256. [AV_SAMPLE_FMT_S64P] = { AV_CODEC_ID_PCM_S64LE, AV_CODEC_ID_PCM_S64BE },
  1257. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  1258. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  1259. };
  1260. if (fmt < 0 || fmt >= FF_ARRAY_ELEMS(map))
  1261. return AV_CODEC_ID_NONE;
  1262. if (be < 0 || be > 1)
  1263. be = AV_NE(1, 0);
  1264. return map[fmt][be];
  1265. }
  1266. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1267. {
  1268. switch (codec_id) {
  1269. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1270. return 2;
  1271. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1272. return 3;
  1273. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1274. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1275. case AV_CODEC_ID_ADPCM_IMA_QT:
  1276. case AV_CODEC_ID_ADPCM_SWF:
  1277. case AV_CODEC_ID_ADPCM_MS:
  1278. return 4;
  1279. default:
  1280. return av_get_exact_bits_per_sample(codec_id);
  1281. }
  1282. }
  1283. static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
  1284. uint32_t tag, int bits_per_coded_sample, int64_t bitrate,
  1285. uint8_t * extradata, int frame_size, int frame_bytes)
  1286. {
  1287. int bps = av_get_exact_bits_per_sample(id);
  1288. int framecount = (ba > 0 && frame_bytes / ba > 0) ? frame_bytes / ba : 1;
  1289. /* codecs with an exact constant bits per sample */
  1290. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  1291. return (frame_bytes * 8LL) / (bps * ch);
  1292. bps = bits_per_coded_sample;
  1293. /* codecs with a fixed packet duration */
  1294. switch (id) {
  1295. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1296. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1297. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1298. case AV_CODEC_ID_AMR_NB:
  1299. case AV_CODEC_ID_EVRC:
  1300. case AV_CODEC_ID_GSM:
  1301. case AV_CODEC_ID_QCELP:
  1302. case AV_CODEC_ID_RA_288: return 160;
  1303. case AV_CODEC_ID_AMR_WB:
  1304. case AV_CODEC_ID_GSM_MS: return 320;
  1305. case AV_CODEC_ID_MP1: return 384;
  1306. case AV_CODEC_ID_ATRAC1: return 512;
  1307. case AV_CODEC_ID_ATRAC9:
  1308. case AV_CODEC_ID_ATRAC3:
  1309. if (framecount > INT_MAX/1024)
  1310. return 0;
  1311. return 1024 * framecount;
  1312. case AV_CODEC_ID_ATRAC3P: return 2048;
  1313. case AV_CODEC_ID_MP2:
  1314. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1315. case AV_CODEC_ID_AC3: return 1536;
  1316. }
  1317. if (sr > 0) {
  1318. /* calc from sample rate */
  1319. if (id == AV_CODEC_ID_TTA)
  1320. return 256 * sr / 245;
  1321. else if (id == AV_CODEC_ID_DST)
  1322. return 588 * sr / 44100;
  1323. else if (id == AV_CODEC_ID_BINKAUDIO_DCT) {
  1324. if (sr / 22050 > 22)
  1325. return 0;
  1326. return (480 << (sr / 22050));
  1327. }
  1328. if (id == AV_CODEC_ID_MP3)
  1329. return sr <= 24000 ? 576 : 1152;
  1330. }
  1331. if (ba > 0) {
  1332. /* calc from block_align */
  1333. if (id == AV_CODEC_ID_SIPR) {
  1334. switch (ba) {
  1335. case 20: return 160;
  1336. case 19: return 144;
  1337. case 29: return 288;
  1338. case 37: return 480;
  1339. }
  1340. } else if (id == AV_CODEC_ID_ILBC) {
  1341. switch (ba) {
  1342. case 38: return 160;
  1343. case 50: return 240;
  1344. }
  1345. }
  1346. }
  1347. if (frame_bytes > 0) {
  1348. /* calc from frame_bytes only */
  1349. if (id == AV_CODEC_ID_TRUESPEECH)
  1350. return 240 * (frame_bytes / 32);
  1351. if (id == AV_CODEC_ID_NELLYMOSER)
  1352. return 256 * (frame_bytes / 64);
  1353. if (id == AV_CODEC_ID_RA_144)
  1354. return 160 * (frame_bytes / 20);
  1355. if (bps > 0) {
  1356. /* calc from frame_bytes and bits_per_coded_sample */
  1357. if (id == AV_CODEC_ID_ADPCM_G726 || id == AV_CODEC_ID_ADPCM_G726LE)
  1358. return frame_bytes * 8 / bps;
  1359. }
  1360. if (ch > 0 && ch < INT_MAX/16) {
  1361. /* calc from frame_bytes and channels */
  1362. switch (id) {
  1363. case AV_CODEC_ID_FASTAUDIO:
  1364. return frame_bytes / (40 * ch) * 256;
  1365. case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
  1366. return (frame_bytes - 4 * ch) / (128 * ch) * 256;
  1367. case AV_CODEC_ID_ADPCM_AFC:
  1368. return frame_bytes / (9 * ch) * 16;
  1369. case AV_CODEC_ID_ADPCM_PSX:
  1370. case AV_CODEC_ID_ADPCM_DTK:
  1371. frame_bytes /= 16 * ch;
  1372. if (frame_bytes > INT_MAX / 28)
  1373. return 0;
  1374. return frame_bytes * 28;
  1375. case AV_CODEC_ID_ADPCM_4XM:
  1376. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  1377. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1378. return (frame_bytes - 4 * ch) * 2 / ch;
  1379. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1380. return (frame_bytes - 4) * 2 / ch;
  1381. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1382. return (frame_bytes - 8) * 2;
  1383. case AV_CODEC_ID_ADPCM_THP:
  1384. case AV_CODEC_ID_ADPCM_THP_LE:
  1385. if (extradata)
  1386. return frame_bytes * 14 / (8 * ch);
  1387. break;
  1388. case AV_CODEC_ID_ADPCM_XA:
  1389. return (frame_bytes / 128) * 224 / ch;
  1390. case AV_CODEC_ID_INTERPLAY_DPCM:
  1391. return (frame_bytes - 6 - ch) / ch;
  1392. case AV_CODEC_ID_ROQ_DPCM:
  1393. return (frame_bytes - 8) / ch;
  1394. case AV_CODEC_ID_XAN_DPCM:
  1395. return (frame_bytes - 2 * ch) / ch;
  1396. case AV_CODEC_ID_MACE3:
  1397. return 3 * frame_bytes / ch;
  1398. case AV_CODEC_ID_MACE6:
  1399. return 6 * frame_bytes / ch;
  1400. case AV_CODEC_ID_PCM_LXF:
  1401. return 2 * (frame_bytes / (5 * ch));
  1402. case AV_CODEC_ID_IAC:
  1403. case AV_CODEC_ID_IMC:
  1404. return 4 * frame_bytes / ch;
  1405. }
  1406. if (tag) {
  1407. /* calc from frame_bytes, channels, and codec_tag */
  1408. if (id == AV_CODEC_ID_SOL_DPCM) {
  1409. if (tag == 3)
  1410. return frame_bytes / ch;
  1411. else
  1412. return frame_bytes * 2 / ch;
  1413. }
  1414. }
  1415. if (ba > 0) {
  1416. /* calc from frame_bytes, channels, and block_align */
  1417. int blocks = frame_bytes / ba;
  1418. switch (id) {
  1419. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1420. if (bps < 2 || bps > 5)
  1421. return 0;
  1422. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  1423. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1424. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1425. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1426. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1427. case AV_CODEC_ID_ADPCM_IMA_RAD:
  1428. return blocks * ((ba - 4 * ch) * 2 / ch);
  1429. case AV_CODEC_ID_ADPCM_MS:
  1430. return blocks * (2 + (ba - 7 * ch) * 2LL / ch);
  1431. case AV_CODEC_ID_ADPCM_MTAF:
  1432. return blocks * (ba - 16) * 2 / ch;
  1433. }
  1434. }
  1435. if (bps > 0) {
  1436. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1437. switch (id) {
  1438. case AV_CODEC_ID_PCM_DVD:
  1439. if(bps<4 || frame_bytes<3)
  1440. return 0;
  1441. return 2 * ((frame_bytes - 3) / ((bps * 2 / 8) * ch));
  1442. case AV_CODEC_ID_PCM_BLURAY:
  1443. if(bps<4 || frame_bytes<4)
  1444. return 0;
  1445. return (frame_bytes - 4) / ((FFALIGN(ch, 2) * bps) / 8);
  1446. case AV_CODEC_ID_S302M:
  1447. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1448. }
  1449. }
  1450. }
  1451. }
  1452. /* Fall back on using frame_size */
  1453. if (frame_size > 1 && frame_bytes)
  1454. return frame_size;
  1455. //For WMA we currently have no other means to calculate duration thus we
  1456. //do it here by assuming CBR, which is true for all known cases.
  1457. if (bitrate > 0 && frame_bytes > 0 && sr > 0 && ba > 1) {
  1458. if (id == AV_CODEC_ID_WMAV1 || id == AV_CODEC_ID_WMAV2)
  1459. return (frame_bytes * 8LL * sr) / bitrate;
  1460. }
  1461. return 0;
  1462. }
  1463. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1464. {
  1465. return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
  1466. avctx->channels, avctx->block_align,
  1467. avctx->codec_tag, avctx->bits_per_coded_sample,
  1468. avctx->bit_rate, avctx->extradata, avctx->frame_size,
  1469. frame_bytes);
  1470. }
  1471. int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
  1472. {
  1473. return get_audio_frame_duration(par->codec_id, par->sample_rate,
  1474. par->channels, par->block_align,
  1475. par->codec_tag, par->bits_per_coded_sample,
  1476. par->bit_rate, par->extradata, par->frame_size,
  1477. frame_bytes);
  1478. }
  1479. #if !HAVE_THREADS
  1480. int ff_thread_init(AVCodecContext *s)
  1481. {
  1482. return -1;
  1483. }
  1484. #endif
  1485. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1486. {
  1487. unsigned int n = 0;
  1488. while (v >= 0xff) {
  1489. *s++ = 0xff;
  1490. v -= 0xff;
  1491. n++;
  1492. }
  1493. *s = v;
  1494. n++;
  1495. return n;
  1496. }
  1497. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1498. {
  1499. int i;
  1500. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1501. return i;
  1502. }
  1503. const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index)
  1504. {
  1505. int i;
  1506. if (!codec->hw_configs || index < 0)
  1507. return NULL;
  1508. for (i = 0; i <= index; i++)
  1509. if (!codec->hw_configs[i])
  1510. return NULL;
  1511. return &codec->hw_configs[index]->public;
  1512. }
  1513. #if FF_API_USER_VISIBLE_AVHWACCEL
  1514. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  1515. {
  1516. return NULL;
  1517. }
  1518. void av_register_hwaccel(AVHWAccel *hwaccel)
  1519. {
  1520. }
  1521. #endif
  1522. #if FF_API_LOCKMGR
  1523. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1524. {
  1525. return 0;
  1526. }
  1527. #endif
  1528. unsigned int avpriv_toupper4(unsigned int x)
  1529. {
  1530. return av_toupper(x & 0xFF) +
  1531. (av_toupper((x >> 8) & 0xFF) << 8) +
  1532. (av_toupper((x >> 16) & 0xFF) << 16) +
  1533. ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
  1534. }
  1535. int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
  1536. {
  1537. int ret;
  1538. dst->owner[0] = src->owner[0];
  1539. dst->owner[1] = src->owner[1];
  1540. ret = av_frame_ref(dst->f, src->f);
  1541. if (ret < 0)
  1542. return ret;
  1543. av_assert0(!dst->progress);
  1544. if (src->progress &&
  1545. !(dst->progress = av_buffer_ref(src->progress))) {
  1546. ff_thread_release_buffer(dst->owner[0], dst);
  1547. return AVERROR(ENOMEM);
  1548. }
  1549. return 0;
  1550. }
  1551. #if !HAVE_THREADS
  1552. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  1553. {
  1554. return ff_get_format(avctx, fmt);
  1555. }
  1556. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  1557. {
  1558. f->owner[0] = f->owner[1] = avctx;
  1559. return ff_get_buffer(avctx, f->f, flags);
  1560. }
  1561. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  1562. {
  1563. if (f->f)
  1564. av_frame_unref(f->f);
  1565. }
  1566. void ff_thread_finish_setup(AVCodecContext *avctx)
  1567. {
  1568. }
  1569. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  1570. {
  1571. }
  1572. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  1573. {
  1574. }
  1575. int ff_thread_can_start_frame(AVCodecContext *avctx)
  1576. {
  1577. return 1;
  1578. }
  1579. int ff_alloc_entries(AVCodecContext *avctx, int count)
  1580. {
  1581. return 0;
  1582. }
  1583. void ff_reset_entries(AVCodecContext *avctx)
  1584. {
  1585. }
  1586. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  1587. {
  1588. }
  1589. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  1590. {
  1591. }
  1592. #endif
  1593. int avcodec_is_open(AVCodecContext *s)
  1594. {
  1595. return !!s->internal;
  1596. }
  1597. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  1598. const uint8_t *end,
  1599. uint32_t *av_restrict state)
  1600. {
  1601. int i;
  1602. av_assert0(p <= end);
  1603. if (p >= end)
  1604. return end;
  1605. for (i = 0; i < 3; i++) {
  1606. uint32_t tmp = *state << 8;
  1607. *state = tmp + *(p++);
  1608. if (tmp == 0x100 || p == end)
  1609. return p;
  1610. }
  1611. while (p < end) {
  1612. if (p[-1] > 1 ) p += 3;
  1613. else if (p[-2] ) p += 2;
  1614. else if (p[-3]|(p[-1]-1)) p++;
  1615. else {
  1616. p++;
  1617. break;
  1618. }
  1619. }
  1620. p = FFMIN(p, end) - 4;
  1621. *state = AV_RB32(p);
  1622. return p + 4;
  1623. }
  1624. AVCPBProperties *av_cpb_properties_alloc(size_t *size)
  1625. {
  1626. AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
  1627. if (!props)
  1628. return NULL;
  1629. if (size)
  1630. *size = sizeof(*props);
  1631. props->vbv_delay = UINT64_MAX;
  1632. return props;
  1633. }
  1634. AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
  1635. {
  1636. AVPacketSideData *tmp;
  1637. AVCPBProperties *props;
  1638. size_t size;
  1639. int i;
  1640. for (i = 0; i < avctx->nb_coded_side_data; i++)
  1641. if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
  1642. return (AVCPBProperties *)avctx->coded_side_data[i].data;
  1643. props = av_cpb_properties_alloc(&size);
  1644. if (!props)
  1645. return NULL;
  1646. tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
  1647. if (!tmp) {
  1648. av_freep(&props);
  1649. return NULL;
  1650. }
  1651. avctx->coded_side_data = tmp;
  1652. avctx->nb_coded_side_data++;
  1653. avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
  1654. avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
  1655. avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
  1656. return props;
  1657. }
  1658. static unsigned bcd2uint(uint8_t bcd)
  1659. {
  1660. unsigned low = bcd & 0xf;
  1661. unsigned high = bcd >> 4;
  1662. if (low > 9 || high > 9)
  1663. return 0;
  1664. return low + 10*high;
  1665. }
  1666. int ff_alloc_timecode_sei(const AVFrame *frame, AVRational rate, size_t prefix_len,
  1667. void **data, size_t *sei_size)
  1668. {
  1669. AVFrameSideData *sd = NULL;
  1670. uint8_t *sei_data;
  1671. PutBitContext pb;
  1672. uint32_t *tc;
  1673. int m;
  1674. if (frame)
  1675. sd = av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE);
  1676. if (!sd) {
  1677. *data = NULL;
  1678. return 0;
  1679. }
  1680. tc = (uint32_t*)sd->data;
  1681. m = tc[0] & 3;
  1682. *sei_size = sizeof(uint32_t) * 4;
  1683. *data = av_mallocz(*sei_size + prefix_len);
  1684. if (!*data)
  1685. return AVERROR(ENOMEM);
  1686. sei_data = (uint8_t*)*data + prefix_len;
  1687. init_put_bits(&pb, sei_data, *sei_size);
  1688. put_bits(&pb, 2, m); // num_clock_ts
  1689. for (int j = 1; j <= m; j++) {
  1690. uint32_t tcsmpte = tc[j];
  1691. unsigned hh = bcd2uint(tcsmpte & 0x3f); // 6-bit hours
  1692. unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f); // 7-bit minutes
  1693. unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f); // 7-bit seconds
  1694. unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f); // 6-bit frames
  1695. unsigned drop = tcsmpte & 1<<30 && !0; // 1-bit drop if not arbitrary bit
  1696. /* Calculate frame number of HEVC by SMPTE ST 12-1:2014 Sec 12.2 if rate > 30FPS */
  1697. if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
  1698. unsigned pc;
  1699. ff *= 2;
  1700. if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
  1701. pc = !!(tcsmpte & 1 << 7);
  1702. else
  1703. pc = !!(tcsmpte & 1 << 23);
  1704. ff = (ff + pc) & 0x7f;
  1705. }
  1706. put_bits(&pb, 1, 1); // clock_timestamp_flag
  1707. put_bits(&pb, 1, 1); // units_field_based_flag
  1708. put_bits(&pb, 5, 0); // counting_type
  1709. put_bits(&pb, 1, 1); // full_timestamp_flag
  1710. put_bits(&pb, 1, 0); // discontinuity_flag
  1711. put_bits(&pb, 1, drop);
  1712. put_bits(&pb, 9, ff);
  1713. put_bits(&pb, 6, ss);
  1714. put_bits(&pb, 6, mm);
  1715. put_bits(&pb, 5, hh);
  1716. put_bits(&pb, 5, 0);
  1717. }
  1718. flush_put_bits(&pb);
  1719. return 0;
  1720. }
  1721. int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
  1722. {
  1723. AVRational framerate = avctx->framerate;
  1724. int bits_per_coded_sample = avctx->bits_per_coded_sample;
  1725. int64_t bitrate;
  1726. if (!(framerate.num && framerate.den))
  1727. framerate = av_inv_q(avctx->time_base);
  1728. if (!(framerate.num && framerate.den))
  1729. return 0;
  1730. if (!bits_per_coded_sample) {
  1731. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1732. bits_per_coded_sample = av_get_bits_per_pixel(desc);
  1733. }
  1734. bitrate = (int64_t)bits_per_coded_sample * avctx->width * avctx->height *
  1735. framerate.num / framerate.den;
  1736. return bitrate;
  1737. }
  1738. int ff_int_from_list_or_default(void *ctx, const char * val_name, int val,
  1739. const int * array_valid_values, int default_value)
  1740. {
  1741. int i = 0, ref_val;
  1742. while (1) {
  1743. ref_val = array_valid_values[i];
  1744. if (ref_val == INT_MAX)
  1745. break;
  1746. if (val == ref_val)
  1747. return val;
  1748. i++;
  1749. }
  1750. /* val is not a valid value */
  1751. av_log(ctx, AV_LOG_DEBUG,
  1752. "%s %d are not supported. Set to default value : %d\n", val_name, val, default_value);
  1753. return default_value;
  1754. }