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.

2171 lines
71KB

  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 ff_lock_avcodec(AVCodecContext *log_ctx, 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 ff_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. ff_lock_avcodec(avctx, 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. ff_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. ff_lock_avcodec(avctx, codec);
  653. if (ret < 0)
  654. goto free_and_end;
  655. }
  656. if (av_codec_is_decoder(avctx->codec)) {
  657. ret = ff_decode_bsfs_init(avctx);
  658. if (ret < 0)
  659. goto free_and_end;
  660. }
  661. if (HAVE_THREADS
  662. && !(avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  663. ret = ff_thread_init(avctx);
  664. if (ret < 0) {
  665. goto free_and_end;
  666. }
  667. }
  668. if (!HAVE_THREADS && !(codec->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
  669. avctx->thread_count = 1;
  670. if (av_codec_is_encoder(avctx->codec))
  671. ret = ff_encode_preinit(avctx);
  672. else
  673. ret = ff_decode_preinit(avctx);
  674. if (ret < 0)
  675. goto free_and_end;
  676. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  677. || avci->frame_thread_encoder)) {
  678. ret = avctx->codec->init(avctx);
  679. if (ret < 0) {
  680. codec_init_ok = -1;
  681. goto free_and_end;
  682. }
  683. codec_init_ok = 1;
  684. }
  685. ret=0;
  686. if (av_codec_is_decoder(avctx->codec)) {
  687. if (!avctx->bit_rate)
  688. avctx->bit_rate = get_bit_rate(avctx);
  689. /* validate channel layout from the decoder */
  690. if (avctx->channel_layout) {
  691. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  692. if (!avctx->channels)
  693. avctx->channels = channels;
  694. else if (channels != avctx->channels) {
  695. char buf[512];
  696. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  697. av_log(avctx, AV_LOG_WARNING,
  698. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  699. "ignoring specified channel layout\n",
  700. buf, channels, avctx->channels);
  701. avctx->channel_layout = 0;
  702. }
  703. }
  704. if (avctx->channels && avctx->channels < 0 ||
  705. avctx->channels > FF_SANE_NB_CHANNELS) {
  706. ret = AVERROR(EINVAL);
  707. goto free_and_end;
  708. }
  709. if (avctx->bits_per_coded_sample < 0) {
  710. ret = AVERROR(EINVAL);
  711. goto free_and_end;
  712. }
  713. if (avctx->sub_charenc) {
  714. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  715. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  716. "supported with subtitles codecs\n");
  717. ret = AVERROR(EINVAL);
  718. goto free_and_end;
  719. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  720. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  721. "subtitles character encoding will be ignored\n",
  722. avctx->codec_descriptor->name);
  723. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  724. } else {
  725. /* input character encoding is set for a text based subtitle
  726. * codec at this point */
  727. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  728. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  729. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  730. #if CONFIG_ICONV
  731. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  732. if (cd == (iconv_t)-1) {
  733. ret = AVERROR(errno);
  734. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  735. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  736. goto free_and_end;
  737. }
  738. iconv_close(cd);
  739. #else
  740. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  741. "conversion needs a libavcodec built with iconv support "
  742. "for this codec\n");
  743. ret = AVERROR(ENOSYS);
  744. goto free_and_end;
  745. #endif
  746. }
  747. }
  748. }
  749. #if FF_API_AVCTX_TIMEBASE
  750. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  751. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  752. #endif
  753. }
  754. if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
  755. av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
  756. }
  757. end:
  758. ff_unlock_avcodec(codec);
  759. if (options) {
  760. av_dict_free(options);
  761. *options = tmp;
  762. }
  763. return ret;
  764. free_and_end:
  765. if (avctx->codec && avctx->codec->close &&
  766. (codec_init_ok > 0 || (codec_init_ok < 0 &&
  767. avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)))
  768. avctx->codec->close(avctx);
  769. if (HAVE_THREADS && avci->thread_ctx)
  770. ff_thread_free(avctx);
  771. if (codec->priv_class && avctx->priv_data)
  772. av_opt_free(avctx->priv_data);
  773. av_opt_free(avctx);
  774. if (av_codec_is_encoder(avctx->codec)) {
  775. #if FF_API_CODED_FRAME
  776. FF_DISABLE_DEPRECATION_WARNINGS
  777. av_frame_free(&avctx->coded_frame);
  778. FF_ENABLE_DEPRECATION_WARNINGS
  779. #endif
  780. av_freep(&avctx->extradata);
  781. avctx->extradata_size = 0;
  782. }
  783. av_dict_free(&tmp);
  784. av_freep(&avctx->priv_data);
  785. av_freep(&avctx->subtitle_header);
  786. #if FF_API_OLD_ENCDEC
  787. av_frame_free(&avci->to_free);
  788. av_frame_free(&avci->compat_decode_frame);
  789. av_packet_free(&avci->compat_encode_packet);
  790. #endif
  791. av_frame_free(&avci->buffer_frame);
  792. av_packet_free(&avci->buffer_pkt);
  793. av_packet_free(&avci->last_pkt_props);
  794. av_fifo_freep(&avci->pkt_props);
  795. av_packet_free(&avci->ds.in_pkt);
  796. av_frame_free(&avci->es.in_frame);
  797. av_bsf_free(&avci->bsf);
  798. av_buffer_unref(&avci->pool);
  799. av_freep(&avci);
  800. avctx->internal = NULL;
  801. avctx->codec = NULL;
  802. goto end;
  803. }
  804. void avcodec_flush_buffers(AVCodecContext *avctx)
  805. {
  806. AVCodecInternal *avci = avctx->internal;
  807. if (av_codec_is_encoder(avctx->codec)) {
  808. int caps = avctx->codec->capabilities;
  809. if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
  810. // Only encoders that explicitly declare support for it can be
  811. // flushed. Otherwise, this is a no-op.
  812. av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
  813. "that doesn't support it\n");
  814. return;
  815. }
  816. // We haven't implemented flushing for frame-threaded encoders.
  817. av_assert0(!(caps & AV_CODEC_CAP_FRAME_THREADS));
  818. }
  819. avci->draining = 0;
  820. avci->draining_done = 0;
  821. avci->nb_draining_errors = 0;
  822. av_frame_unref(avci->buffer_frame);
  823. #if FF_API_OLD_ENCDEC
  824. av_frame_unref(avci->compat_decode_frame);
  825. av_packet_unref(avci->compat_encode_packet);
  826. #endif
  827. av_packet_unref(avci->buffer_pkt);
  828. av_packet_unref(avci->last_pkt_props);
  829. while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
  830. av_fifo_generic_read(avci->pkt_props,
  831. avci->last_pkt_props, sizeof(*avci->last_pkt_props),
  832. NULL);
  833. av_packet_unref(avci->last_pkt_props);
  834. }
  835. av_fifo_reset(avci->pkt_props);
  836. av_frame_unref(avci->es.in_frame);
  837. av_packet_unref(avci->ds.in_pkt);
  838. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  839. ff_thread_flush(avctx);
  840. else if (avctx->codec->flush)
  841. avctx->codec->flush(avctx);
  842. avctx->pts_correction_last_pts =
  843. avctx->pts_correction_last_dts = INT64_MIN;
  844. if (av_codec_is_decoder(avctx->codec))
  845. av_bsf_flush(avci->bsf);
  846. #if FF_API_OLD_ENCDEC
  847. FF_DISABLE_DEPRECATION_WARNINGS
  848. if (!avctx->refcounted_frames)
  849. av_frame_unref(avci->to_free);
  850. FF_ENABLE_DEPRECATION_WARNINGS
  851. #endif
  852. }
  853. void avsubtitle_free(AVSubtitle *sub)
  854. {
  855. int i;
  856. for (i = 0; i < sub->num_rects; i++) {
  857. av_freep(&sub->rects[i]->data[0]);
  858. av_freep(&sub->rects[i]->data[1]);
  859. av_freep(&sub->rects[i]->data[2]);
  860. av_freep(&sub->rects[i]->data[3]);
  861. av_freep(&sub->rects[i]->text);
  862. av_freep(&sub->rects[i]->ass);
  863. av_freep(&sub->rects[i]);
  864. }
  865. av_freep(&sub->rects);
  866. memset(sub, 0, sizeof(*sub));
  867. }
  868. av_cold int avcodec_close(AVCodecContext *avctx)
  869. {
  870. int i;
  871. if (!avctx)
  872. return 0;
  873. if (avcodec_is_open(avctx)) {
  874. if (CONFIG_FRAME_THREAD_ENCODER &&
  875. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  876. ff_frame_thread_encoder_free(avctx);
  877. }
  878. if (HAVE_THREADS && avctx->internal->thread_ctx)
  879. ff_thread_free(avctx);
  880. if (avctx->codec && avctx->codec->close)
  881. avctx->codec->close(avctx);
  882. avctx->internal->byte_buffer_size = 0;
  883. av_freep(&avctx->internal->byte_buffer);
  884. #if FF_API_OLD_ENCDEC
  885. av_frame_free(&avctx->internal->to_free);
  886. av_frame_free(&avctx->internal->compat_decode_frame);
  887. av_packet_free(&avctx->internal->compat_encode_packet);
  888. #endif
  889. av_frame_free(&avctx->internal->buffer_frame);
  890. av_packet_free(&avctx->internal->buffer_pkt);
  891. av_packet_unref(avctx->internal->last_pkt_props);
  892. while (av_fifo_size(avctx->internal->pkt_props) >=
  893. sizeof(*avctx->internal->last_pkt_props)) {
  894. av_fifo_generic_read(avctx->internal->pkt_props,
  895. avctx->internal->last_pkt_props,
  896. sizeof(*avctx->internal->last_pkt_props),
  897. NULL);
  898. av_packet_unref(avctx->internal->last_pkt_props);
  899. }
  900. av_packet_free(&avctx->internal->last_pkt_props);
  901. av_fifo_freep(&avctx->internal->pkt_props);
  902. av_packet_free(&avctx->internal->ds.in_pkt);
  903. av_frame_free(&avctx->internal->es.in_frame);
  904. av_buffer_unref(&avctx->internal->pool);
  905. if (avctx->hwaccel && avctx->hwaccel->uninit)
  906. avctx->hwaccel->uninit(avctx);
  907. av_freep(&avctx->internal->hwaccel_priv_data);
  908. av_bsf_free(&avctx->internal->bsf);
  909. av_freep(&avctx->internal);
  910. }
  911. for (i = 0; i < avctx->nb_coded_side_data; i++)
  912. av_freep(&avctx->coded_side_data[i].data);
  913. av_freep(&avctx->coded_side_data);
  914. avctx->nb_coded_side_data = 0;
  915. av_buffer_unref(&avctx->hw_frames_ctx);
  916. av_buffer_unref(&avctx->hw_device_ctx);
  917. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  918. av_opt_free(avctx->priv_data);
  919. av_opt_free(avctx);
  920. av_freep(&avctx->priv_data);
  921. if (av_codec_is_encoder(avctx->codec)) {
  922. av_freep(&avctx->extradata);
  923. #if FF_API_CODED_FRAME
  924. FF_DISABLE_DEPRECATION_WARNINGS
  925. av_frame_free(&avctx->coded_frame);
  926. FF_ENABLE_DEPRECATION_WARNINGS
  927. #endif
  928. }
  929. avctx->codec = NULL;
  930. avctx->active_thread_type = 0;
  931. return 0;
  932. }
  933. const char *avcodec_get_name(enum AVCodecID id)
  934. {
  935. const AVCodecDescriptor *cd;
  936. const AVCodec *codec;
  937. if (id == AV_CODEC_ID_NONE)
  938. return "none";
  939. cd = avcodec_descriptor_get(id);
  940. if (cd)
  941. return cd->name;
  942. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  943. codec = avcodec_find_decoder(id);
  944. if (codec)
  945. return codec->name;
  946. codec = avcodec_find_encoder(id);
  947. if (codec)
  948. return codec->name;
  949. return "unknown_codec";
  950. }
  951. #if FF_API_TAG_STRING
  952. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  953. {
  954. int i, len, ret = 0;
  955. #define TAG_PRINT(x) \
  956. (((x) >= '0' && (x) <= '9') || \
  957. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  958. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  959. for (i = 0; i < 4; i++) {
  960. len = snprintf(buf, buf_size,
  961. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  962. buf += len;
  963. buf_size = buf_size > len ? buf_size - len : 0;
  964. ret += len;
  965. codec_tag >>= 8;
  966. }
  967. return ret;
  968. }
  969. #endif
  970. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  971. {
  972. const char *codec_type;
  973. const char *codec_name;
  974. const char *profile = NULL;
  975. int64_t bitrate;
  976. int new_line = 0;
  977. AVRational display_aspect_ratio;
  978. const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
  979. if (!buf || buf_size <= 0)
  980. return;
  981. codec_type = av_get_media_type_string(enc->codec_type);
  982. codec_name = avcodec_get_name(enc->codec_id);
  983. profile = avcodec_profile_name(enc->codec_id, enc->profile);
  984. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  985. codec_name);
  986. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  987. if (enc->codec && strcmp(enc->codec->name, codec_name))
  988. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  989. if (profile)
  990. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  991. if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
  992. && av_log_get_level() >= AV_LOG_VERBOSE
  993. && enc->refs)
  994. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  995. ", %d reference frame%s",
  996. enc->refs, enc->refs > 1 ? "s" : "");
  997. if (enc->codec_tag)
  998. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s / 0x%04X)",
  999. av_fourcc2str(enc->codec_tag), enc->codec_tag);
  1000. switch (enc->codec_type) {
  1001. case AVMEDIA_TYPE_VIDEO:
  1002. {
  1003. char detail[256] = "(";
  1004. av_strlcat(buf, separator, buf_size);
  1005. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1006. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  1007. av_get_pix_fmt_name(enc->pix_fmt));
  1008. if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
  1009. enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
  1010. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  1011. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  1012. av_strlcatf(detail, sizeof(detail), "%s, ",
  1013. av_color_range_name(enc->color_range));
  1014. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  1015. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  1016. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  1017. if (enc->colorspace != (int)enc->color_primaries ||
  1018. enc->colorspace != (int)enc->color_trc) {
  1019. new_line = 1;
  1020. av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
  1021. av_color_space_name(enc->colorspace),
  1022. av_color_primaries_name(enc->color_primaries),
  1023. av_color_transfer_name(enc->color_trc));
  1024. } else
  1025. av_strlcatf(detail, sizeof(detail), "%s, ",
  1026. av_get_colorspace_name(enc->colorspace));
  1027. }
  1028. if (enc->field_order != AV_FIELD_UNKNOWN) {
  1029. const char *field_order = "progressive";
  1030. if (enc->field_order == AV_FIELD_TT)
  1031. field_order = "top first";
  1032. else if (enc->field_order == AV_FIELD_BB)
  1033. field_order = "bottom first";
  1034. else if (enc->field_order == AV_FIELD_TB)
  1035. field_order = "top coded first (swapped)";
  1036. else if (enc->field_order == AV_FIELD_BT)
  1037. field_order = "bottom coded first (swapped)";
  1038. av_strlcatf(detail, sizeof(detail), "%s, ", field_order);
  1039. }
  1040. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  1041. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  1042. av_strlcatf(detail, sizeof(detail), "%s, ",
  1043. av_chroma_location_name(enc->chroma_sample_location));
  1044. if (strlen(detail) > 1) {
  1045. detail[strlen(detail) - 2] = 0;
  1046. av_strlcatf(buf, buf_size, "%s)", detail);
  1047. }
  1048. }
  1049. if (enc->width) {
  1050. av_strlcat(buf, new_line ? separator : ", ", buf_size);
  1051. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1052. "%dx%d",
  1053. enc->width, enc->height);
  1054. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  1055. (enc->width != enc->coded_width ||
  1056. enc->height != enc->coded_height))
  1057. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1058. " (%dx%d)", enc->coded_width, enc->coded_height);
  1059. if (enc->sample_aspect_ratio.num) {
  1060. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1061. enc->width * (int64_t)enc->sample_aspect_ratio.num,
  1062. enc->height * (int64_t)enc->sample_aspect_ratio.den,
  1063. 1024 * 1024);
  1064. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1065. " [SAR %d:%d DAR %d:%d]",
  1066. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1067. display_aspect_ratio.num, display_aspect_ratio.den);
  1068. }
  1069. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1070. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1071. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1072. ", %d/%d",
  1073. enc->time_base.num / g, enc->time_base.den / g);
  1074. }
  1075. }
  1076. if (encode) {
  1077. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1078. ", q=%d-%d", enc->qmin, enc->qmax);
  1079. } else {
  1080. if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
  1081. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1082. ", Closed Captions");
  1083. if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
  1084. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1085. ", lossless");
  1086. }
  1087. break;
  1088. case AVMEDIA_TYPE_AUDIO:
  1089. av_strlcat(buf, separator, buf_size);
  1090. if (enc->sample_rate) {
  1091. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1092. "%d Hz, ", enc->sample_rate);
  1093. }
  1094. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1095. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1096. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1097. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1098. }
  1099. if ( enc->bits_per_raw_sample > 0
  1100. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  1101. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1102. " (%d bit)", enc->bits_per_raw_sample);
  1103. if (av_log_get_level() >= AV_LOG_VERBOSE) {
  1104. if (enc->initial_padding)
  1105. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1106. ", delay %d", enc->initial_padding);
  1107. if (enc->trailing_padding)
  1108. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1109. ", padding %d", enc->trailing_padding);
  1110. }
  1111. break;
  1112. case AVMEDIA_TYPE_DATA:
  1113. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1114. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1115. if (g)
  1116. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1117. ", %d/%d",
  1118. enc->time_base.num / g, enc->time_base.den / g);
  1119. }
  1120. break;
  1121. case AVMEDIA_TYPE_SUBTITLE:
  1122. if (enc->width)
  1123. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1124. ", %dx%d", enc->width, enc->height);
  1125. break;
  1126. default:
  1127. return;
  1128. }
  1129. if (encode) {
  1130. if (enc->flags & AV_CODEC_FLAG_PASS1)
  1131. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1132. ", pass 1");
  1133. if (enc->flags & AV_CODEC_FLAG_PASS2)
  1134. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1135. ", pass 2");
  1136. }
  1137. bitrate = get_bit_rate(enc);
  1138. if (bitrate != 0) {
  1139. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1140. ", %"PRId64" kb/s", bitrate / 1000);
  1141. } else if (enc->rc_max_rate > 0) {
  1142. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1143. ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
  1144. }
  1145. }
  1146. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1147. {
  1148. const AVProfile *p;
  1149. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1150. return NULL;
  1151. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1152. if (p->profile == profile)
  1153. return p->name;
  1154. return NULL;
  1155. }
  1156. const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
  1157. {
  1158. const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
  1159. const AVProfile *p;
  1160. if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
  1161. return NULL;
  1162. for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1163. if (p->profile == profile)
  1164. return p->name;
  1165. return NULL;
  1166. }
  1167. unsigned avcodec_version(void)
  1168. {
  1169. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  1170. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  1171. av_assert0(AV_CODEC_ID_SRT==94216);
  1172. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1173. return LIBAVCODEC_VERSION_INT;
  1174. }
  1175. const char *avcodec_configuration(void)
  1176. {
  1177. return FFMPEG_CONFIGURATION;
  1178. }
  1179. const char *avcodec_license(void)
  1180. {
  1181. #define LICENSE_PREFIX "libavcodec license: "
  1182. return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
  1183. }
  1184. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1185. {
  1186. switch (codec_id) {
  1187. case AV_CODEC_ID_8SVX_EXP:
  1188. case AV_CODEC_ID_8SVX_FIB:
  1189. case AV_CODEC_ID_ADPCM_ARGO:
  1190. case AV_CODEC_ID_ADPCM_CT:
  1191. case AV_CODEC_ID_ADPCM_IMA_ALP:
  1192. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1193. case AV_CODEC_ID_ADPCM_IMA_APC:
  1194. case AV_CODEC_ID_ADPCM_IMA_APM:
  1195. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1196. case AV_CODEC_ID_ADPCM_IMA_OKI:
  1197. case AV_CODEC_ID_ADPCM_IMA_WS:
  1198. case AV_CODEC_ID_ADPCM_IMA_SSI:
  1199. case AV_CODEC_ID_ADPCM_G722:
  1200. case AV_CODEC_ID_ADPCM_YAMAHA:
  1201. case AV_CODEC_ID_ADPCM_AICA:
  1202. return 4;
  1203. case AV_CODEC_ID_DSD_LSBF:
  1204. case AV_CODEC_ID_DSD_MSBF:
  1205. case AV_CODEC_ID_DSD_LSBF_PLANAR:
  1206. case AV_CODEC_ID_DSD_MSBF_PLANAR:
  1207. case AV_CODEC_ID_PCM_ALAW:
  1208. case AV_CODEC_ID_PCM_MULAW:
  1209. case AV_CODEC_ID_PCM_VIDC:
  1210. case AV_CODEC_ID_PCM_S8:
  1211. case AV_CODEC_ID_PCM_S8_PLANAR:
  1212. case AV_CODEC_ID_PCM_SGA:
  1213. case AV_CODEC_ID_PCM_U8:
  1214. case AV_CODEC_ID_SDX2_DPCM:
  1215. case AV_CODEC_ID_DERF_DPCM:
  1216. return 8;
  1217. case AV_CODEC_ID_PCM_S16BE:
  1218. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  1219. case AV_CODEC_ID_PCM_S16LE:
  1220. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1221. case AV_CODEC_ID_PCM_U16BE:
  1222. case AV_CODEC_ID_PCM_U16LE:
  1223. return 16;
  1224. case AV_CODEC_ID_PCM_S24DAUD:
  1225. case AV_CODEC_ID_PCM_S24BE:
  1226. case AV_CODEC_ID_PCM_S24LE:
  1227. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  1228. case AV_CODEC_ID_PCM_U24BE:
  1229. case AV_CODEC_ID_PCM_U24LE:
  1230. return 24;
  1231. case AV_CODEC_ID_PCM_S32BE:
  1232. case AV_CODEC_ID_PCM_S32LE:
  1233. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  1234. case AV_CODEC_ID_PCM_U32BE:
  1235. case AV_CODEC_ID_PCM_U32LE:
  1236. case AV_CODEC_ID_PCM_F32BE:
  1237. case AV_CODEC_ID_PCM_F32LE:
  1238. case AV_CODEC_ID_PCM_F24LE:
  1239. case AV_CODEC_ID_PCM_F16LE:
  1240. return 32;
  1241. case AV_CODEC_ID_PCM_F64BE:
  1242. case AV_CODEC_ID_PCM_F64LE:
  1243. case AV_CODEC_ID_PCM_S64BE:
  1244. case AV_CODEC_ID_PCM_S64LE:
  1245. return 64;
  1246. default:
  1247. return 0;
  1248. }
  1249. }
  1250. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1251. {
  1252. static const enum AVCodecID map[][2] = {
  1253. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  1254. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  1255. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  1256. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  1257. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  1258. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  1259. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  1260. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  1261. [AV_SAMPLE_FMT_S64P] = { AV_CODEC_ID_PCM_S64LE, AV_CODEC_ID_PCM_S64BE },
  1262. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  1263. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  1264. };
  1265. if (fmt < 0 || fmt >= FF_ARRAY_ELEMS(map))
  1266. return AV_CODEC_ID_NONE;
  1267. if (be < 0 || be > 1)
  1268. be = AV_NE(1, 0);
  1269. return map[fmt][be];
  1270. }
  1271. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1272. {
  1273. switch (codec_id) {
  1274. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1275. return 2;
  1276. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1277. return 3;
  1278. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1279. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1280. case AV_CODEC_ID_ADPCM_IMA_QT:
  1281. case AV_CODEC_ID_ADPCM_SWF:
  1282. case AV_CODEC_ID_ADPCM_MS:
  1283. return 4;
  1284. default:
  1285. return av_get_exact_bits_per_sample(codec_id);
  1286. }
  1287. }
  1288. static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
  1289. uint32_t tag, int bits_per_coded_sample, int64_t bitrate,
  1290. uint8_t * extradata, int frame_size, int frame_bytes)
  1291. {
  1292. int bps = av_get_exact_bits_per_sample(id);
  1293. int framecount = (ba > 0 && frame_bytes / ba > 0) ? frame_bytes / ba : 1;
  1294. /* codecs with an exact constant bits per sample */
  1295. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  1296. return (frame_bytes * 8LL) / (bps * ch);
  1297. bps = bits_per_coded_sample;
  1298. /* codecs with a fixed packet duration */
  1299. switch (id) {
  1300. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1301. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1302. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1303. case AV_CODEC_ID_AMR_NB:
  1304. case AV_CODEC_ID_EVRC:
  1305. case AV_CODEC_ID_GSM:
  1306. case AV_CODEC_ID_QCELP:
  1307. case AV_CODEC_ID_RA_288: return 160;
  1308. case AV_CODEC_ID_AMR_WB:
  1309. case AV_CODEC_ID_GSM_MS: return 320;
  1310. case AV_CODEC_ID_MP1: return 384;
  1311. case AV_CODEC_ID_ATRAC1: return 512;
  1312. case AV_CODEC_ID_ATRAC9:
  1313. case AV_CODEC_ID_ATRAC3:
  1314. if (framecount > INT_MAX/1024)
  1315. return 0;
  1316. return 1024 * framecount;
  1317. case AV_CODEC_ID_ATRAC3P: return 2048;
  1318. case AV_CODEC_ID_MP2:
  1319. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1320. case AV_CODEC_ID_AC3: return 1536;
  1321. }
  1322. if (sr > 0) {
  1323. /* calc from sample rate */
  1324. if (id == AV_CODEC_ID_TTA)
  1325. return 256 * sr / 245;
  1326. else if (id == AV_CODEC_ID_DST)
  1327. return 588 * sr / 44100;
  1328. else if (id == AV_CODEC_ID_BINKAUDIO_DCT) {
  1329. if (sr / 22050 > 22)
  1330. return 0;
  1331. return (480 << (sr / 22050));
  1332. }
  1333. if (id == AV_CODEC_ID_MP3)
  1334. return sr <= 24000 ? 576 : 1152;
  1335. }
  1336. if (ba > 0) {
  1337. /* calc from block_align */
  1338. if (id == AV_CODEC_ID_SIPR) {
  1339. switch (ba) {
  1340. case 20: return 160;
  1341. case 19: return 144;
  1342. case 29: return 288;
  1343. case 37: return 480;
  1344. }
  1345. } else if (id == AV_CODEC_ID_ILBC) {
  1346. switch (ba) {
  1347. case 38: return 160;
  1348. case 50: return 240;
  1349. }
  1350. }
  1351. }
  1352. if (frame_bytes > 0) {
  1353. /* calc from frame_bytes only */
  1354. if (id == AV_CODEC_ID_TRUESPEECH)
  1355. return 240 * (frame_bytes / 32);
  1356. if (id == AV_CODEC_ID_NELLYMOSER)
  1357. return 256 * (frame_bytes / 64);
  1358. if (id == AV_CODEC_ID_RA_144)
  1359. return 160 * (frame_bytes / 20);
  1360. if (bps > 0) {
  1361. /* calc from frame_bytes and bits_per_coded_sample */
  1362. if (id == AV_CODEC_ID_ADPCM_G726 || id == AV_CODEC_ID_ADPCM_G726LE)
  1363. return frame_bytes * 8 / bps;
  1364. }
  1365. if (ch > 0 && ch < INT_MAX/16) {
  1366. /* calc from frame_bytes and channels */
  1367. switch (id) {
  1368. case AV_CODEC_ID_FASTAUDIO:
  1369. return frame_bytes / (40 * ch) * 256;
  1370. case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
  1371. return (frame_bytes - 4 * ch) / (128 * ch) * 256;
  1372. case AV_CODEC_ID_ADPCM_AFC:
  1373. return frame_bytes / (9 * ch) * 16;
  1374. case AV_CODEC_ID_ADPCM_PSX:
  1375. case AV_CODEC_ID_ADPCM_DTK:
  1376. frame_bytes /= 16 * ch;
  1377. if (frame_bytes > INT_MAX / 28)
  1378. return 0;
  1379. return frame_bytes * 28;
  1380. case AV_CODEC_ID_ADPCM_4XM:
  1381. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  1382. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1383. return (frame_bytes - 4 * ch) * 2 / ch;
  1384. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1385. return (frame_bytes - 4) * 2 / ch;
  1386. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1387. return (frame_bytes - 8) * 2;
  1388. case AV_CODEC_ID_ADPCM_THP:
  1389. case AV_CODEC_ID_ADPCM_THP_LE:
  1390. if (extradata)
  1391. return frame_bytes * 14 / (8 * ch);
  1392. break;
  1393. case AV_CODEC_ID_ADPCM_XA:
  1394. return (frame_bytes / 128) * 224 / ch;
  1395. case AV_CODEC_ID_INTERPLAY_DPCM:
  1396. return (frame_bytes - 6 - ch) / ch;
  1397. case AV_CODEC_ID_ROQ_DPCM:
  1398. return (frame_bytes - 8) / ch;
  1399. case AV_CODEC_ID_XAN_DPCM:
  1400. return (frame_bytes - 2 * ch) / ch;
  1401. case AV_CODEC_ID_MACE3:
  1402. return 3 * frame_bytes / ch;
  1403. case AV_CODEC_ID_MACE6:
  1404. return 6 * frame_bytes / ch;
  1405. case AV_CODEC_ID_PCM_LXF:
  1406. return 2 * (frame_bytes / (5 * ch));
  1407. case AV_CODEC_ID_IAC:
  1408. case AV_CODEC_ID_IMC:
  1409. return 4 * frame_bytes / ch;
  1410. }
  1411. if (tag) {
  1412. /* calc from frame_bytes, channels, and codec_tag */
  1413. if (id == AV_CODEC_ID_SOL_DPCM) {
  1414. if (tag == 3)
  1415. return frame_bytes / ch;
  1416. else
  1417. return frame_bytes * 2 / ch;
  1418. }
  1419. }
  1420. if (ba > 0) {
  1421. /* calc from frame_bytes, channels, and block_align */
  1422. int blocks = frame_bytes / ba;
  1423. switch (id) {
  1424. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1425. if (bps < 2 || bps > 5)
  1426. return 0;
  1427. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  1428. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1429. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1430. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1431. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1432. case AV_CODEC_ID_ADPCM_IMA_RAD:
  1433. return blocks * ((ba - 4 * ch) * 2 / ch);
  1434. case AV_CODEC_ID_ADPCM_MS:
  1435. return blocks * (2 + (ba - 7 * ch) * 2LL / ch);
  1436. case AV_CODEC_ID_ADPCM_MTAF:
  1437. return blocks * (ba - 16) * 2 / ch;
  1438. }
  1439. }
  1440. if (bps > 0) {
  1441. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1442. switch (id) {
  1443. case AV_CODEC_ID_PCM_DVD:
  1444. if(bps<4 || frame_bytes<3)
  1445. return 0;
  1446. return 2 * ((frame_bytes - 3) / ((bps * 2 / 8) * ch));
  1447. case AV_CODEC_ID_PCM_BLURAY:
  1448. if(bps<4 || frame_bytes<4)
  1449. return 0;
  1450. return (frame_bytes - 4) / ((FFALIGN(ch, 2) * bps) / 8);
  1451. case AV_CODEC_ID_S302M:
  1452. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1453. }
  1454. }
  1455. }
  1456. }
  1457. /* Fall back on using frame_size */
  1458. if (frame_size > 1 && frame_bytes)
  1459. return frame_size;
  1460. //For WMA we currently have no other means to calculate duration thus we
  1461. //do it here by assuming CBR, which is true for all known cases.
  1462. if (bitrate > 0 && frame_bytes > 0 && sr > 0 && ba > 1) {
  1463. if (id == AV_CODEC_ID_WMAV1 || id == AV_CODEC_ID_WMAV2)
  1464. return (frame_bytes * 8LL * sr) / bitrate;
  1465. }
  1466. return 0;
  1467. }
  1468. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1469. {
  1470. return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
  1471. avctx->channels, avctx->block_align,
  1472. avctx->codec_tag, avctx->bits_per_coded_sample,
  1473. avctx->bit_rate, avctx->extradata, avctx->frame_size,
  1474. frame_bytes);
  1475. }
  1476. int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
  1477. {
  1478. return get_audio_frame_duration(par->codec_id, par->sample_rate,
  1479. par->channels, par->block_align,
  1480. par->codec_tag, par->bits_per_coded_sample,
  1481. par->bit_rate, par->extradata, par->frame_size,
  1482. frame_bytes);
  1483. }
  1484. #if !HAVE_THREADS
  1485. int ff_thread_init(AVCodecContext *s)
  1486. {
  1487. return -1;
  1488. }
  1489. #endif
  1490. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1491. {
  1492. unsigned int n = 0;
  1493. while (v >= 0xff) {
  1494. *s++ = 0xff;
  1495. v -= 0xff;
  1496. n++;
  1497. }
  1498. *s = v;
  1499. n++;
  1500. return n;
  1501. }
  1502. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1503. {
  1504. int i;
  1505. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1506. return i;
  1507. }
  1508. const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index)
  1509. {
  1510. int i;
  1511. if (!codec->hw_configs || index < 0)
  1512. return NULL;
  1513. for (i = 0; i <= index; i++)
  1514. if (!codec->hw_configs[i])
  1515. return NULL;
  1516. return &codec->hw_configs[index]->public;
  1517. }
  1518. #if FF_API_USER_VISIBLE_AVHWACCEL
  1519. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  1520. {
  1521. return NULL;
  1522. }
  1523. void av_register_hwaccel(AVHWAccel *hwaccel)
  1524. {
  1525. }
  1526. #endif
  1527. #if FF_API_LOCKMGR
  1528. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1529. {
  1530. return 0;
  1531. }
  1532. #endif
  1533. unsigned int avpriv_toupper4(unsigned int x)
  1534. {
  1535. return av_toupper(x & 0xFF) +
  1536. (av_toupper((x >> 8) & 0xFF) << 8) +
  1537. (av_toupper((x >> 16) & 0xFF) << 16) +
  1538. ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
  1539. }
  1540. int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
  1541. {
  1542. int ret;
  1543. dst->owner[0] = src->owner[0];
  1544. dst->owner[1] = src->owner[1];
  1545. ret = av_frame_ref(dst->f, src->f);
  1546. if (ret < 0)
  1547. return ret;
  1548. av_assert0(!dst->progress);
  1549. if (src->progress &&
  1550. !(dst->progress = av_buffer_ref(src->progress))) {
  1551. ff_thread_release_buffer(dst->owner[0], dst);
  1552. return AVERROR(ENOMEM);
  1553. }
  1554. return 0;
  1555. }
  1556. #if !HAVE_THREADS
  1557. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  1558. {
  1559. return ff_get_format(avctx, fmt);
  1560. }
  1561. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  1562. {
  1563. f->owner[0] = f->owner[1] = avctx;
  1564. return ff_get_buffer(avctx, f->f, flags);
  1565. }
  1566. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  1567. {
  1568. if (f->f)
  1569. av_frame_unref(f->f);
  1570. }
  1571. void ff_thread_finish_setup(AVCodecContext *avctx)
  1572. {
  1573. }
  1574. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  1575. {
  1576. }
  1577. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  1578. {
  1579. }
  1580. int ff_thread_can_start_frame(AVCodecContext *avctx)
  1581. {
  1582. return 1;
  1583. }
  1584. int ff_alloc_entries(AVCodecContext *avctx, int count)
  1585. {
  1586. return 0;
  1587. }
  1588. void ff_reset_entries(AVCodecContext *avctx)
  1589. {
  1590. }
  1591. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  1592. {
  1593. }
  1594. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  1595. {
  1596. }
  1597. #endif
  1598. int avcodec_is_open(AVCodecContext *s)
  1599. {
  1600. return !!s->internal;
  1601. }
  1602. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  1603. const uint8_t *end,
  1604. uint32_t *av_restrict state)
  1605. {
  1606. int i;
  1607. av_assert0(p <= end);
  1608. if (p >= end)
  1609. return end;
  1610. for (i = 0; i < 3; i++) {
  1611. uint32_t tmp = *state << 8;
  1612. *state = tmp + *(p++);
  1613. if (tmp == 0x100 || p == end)
  1614. return p;
  1615. }
  1616. while (p < end) {
  1617. if (p[-1] > 1 ) p += 3;
  1618. else if (p[-2] ) p += 2;
  1619. else if (p[-3]|(p[-1]-1)) p++;
  1620. else {
  1621. p++;
  1622. break;
  1623. }
  1624. }
  1625. p = FFMIN(p, end) - 4;
  1626. *state = AV_RB32(p);
  1627. return p + 4;
  1628. }
  1629. AVCPBProperties *av_cpb_properties_alloc(size_t *size)
  1630. {
  1631. AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
  1632. if (!props)
  1633. return NULL;
  1634. if (size)
  1635. *size = sizeof(*props);
  1636. props->vbv_delay = UINT64_MAX;
  1637. return props;
  1638. }
  1639. AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
  1640. {
  1641. AVPacketSideData *tmp;
  1642. AVCPBProperties *props;
  1643. size_t size;
  1644. int i;
  1645. for (i = 0; i < avctx->nb_coded_side_data; i++)
  1646. if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
  1647. return (AVCPBProperties *)avctx->coded_side_data[i].data;
  1648. props = av_cpb_properties_alloc(&size);
  1649. if (!props)
  1650. return NULL;
  1651. tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
  1652. if (!tmp) {
  1653. av_freep(&props);
  1654. return NULL;
  1655. }
  1656. avctx->coded_side_data = tmp;
  1657. avctx->nb_coded_side_data++;
  1658. avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
  1659. avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
  1660. avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
  1661. return props;
  1662. }
  1663. static void codec_parameters_reset(AVCodecParameters *par)
  1664. {
  1665. av_freep(&par->extradata);
  1666. memset(par, 0, sizeof(*par));
  1667. par->codec_type = AVMEDIA_TYPE_UNKNOWN;
  1668. par->codec_id = AV_CODEC_ID_NONE;
  1669. par->format = -1;
  1670. par->field_order = AV_FIELD_UNKNOWN;
  1671. par->color_range = AVCOL_RANGE_UNSPECIFIED;
  1672. par->color_primaries = AVCOL_PRI_UNSPECIFIED;
  1673. par->color_trc = AVCOL_TRC_UNSPECIFIED;
  1674. par->color_space = AVCOL_SPC_UNSPECIFIED;
  1675. par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  1676. par->sample_aspect_ratio = (AVRational){ 0, 1 };
  1677. par->profile = FF_PROFILE_UNKNOWN;
  1678. par->level = FF_LEVEL_UNKNOWN;
  1679. }
  1680. AVCodecParameters *avcodec_parameters_alloc(void)
  1681. {
  1682. AVCodecParameters *par = av_mallocz(sizeof(*par));
  1683. if (!par)
  1684. return NULL;
  1685. codec_parameters_reset(par);
  1686. return par;
  1687. }
  1688. void avcodec_parameters_free(AVCodecParameters **ppar)
  1689. {
  1690. AVCodecParameters *par = *ppar;
  1691. if (!par)
  1692. return;
  1693. codec_parameters_reset(par);
  1694. av_freep(ppar);
  1695. }
  1696. int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
  1697. {
  1698. codec_parameters_reset(dst);
  1699. memcpy(dst, src, sizeof(*dst));
  1700. dst->extradata = NULL;
  1701. dst->extradata_size = 0;
  1702. if (src->extradata) {
  1703. dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1704. if (!dst->extradata)
  1705. return AVERROR(ENOMEM);
  1706. memcpy(dst->extradata, src->extradata, src->extradata_size);
  1707. dst->extradata_size = src->extradata_size;
  1708. }
  1709. return 0;
  1710. }
  1711. int avcodec_parameters_from_context(AVCodecParameters *par,
  1712. const AVCodecContext *codec)
  1713. {
  1714. codec_parameters_reset(par);
  1715. par->codec_type = codec->codec_type;
  1716. par->codec_id = codec->codec_id;
  1717. par->codec_tag = codec->codec_tag;
  1718. par->bit_rate = codec->bit_rate;
  1719. par->bits_per_coded_sample = codec->bits_per_coded_sample;
  1720. par->bits_per_raw_sample = codec->bits_per_raw_sample;
  1721. par->profile = codec->profile;
  1722. par->level = codec->level;
  1723. switch (par->codec_type) {
  1724. case AVMEDIA_TYPE_VIDEO:
  1725. par->format = codec->pix_fmt;
  1726. par->width = codec->width;
  1727. par->height = codec->height;
  1728. par->field_order = codec->field_order;
  1729. par->color_range = codec->color_range;
  1730. par->color_primaries = codec->color_primaries;
  1731. par->color_trc = codec->color_trc;
  1732. par->color_space = codec->colorspace;
  1733. par->chroma_location = codec->chroma_sample_location;
  1734. par->sample_aspect_ratio = codec->sample_aspect_ratio;
  1735. par->video_delay = codec->has_b_frames;
  1736. break;
  1737. case AVMEDIA_TYPE_AUDIO:
  1738. par->format = codec->sample_fmt;
  1739. par->channel_layout = codec->channel_layout;
  1740. par->channels = codec->channels;
  1741. par->sample_rate = codec->sample_rate;
  1742. par->block_align = codec->block_align;
  1743. par->frame_size = codec->frame_size;
  1744. par->initial_padding = codec->initial_padding;
  1745. par->trailing_padding = codec->trailing_padding;
  1746. par->seek_preroll = codec->seek_preroll;
  1747. break;
  1748. case AVMEDIA_TYPE_SUBTITLE:
  1749. par->width = codec->width;
  1750. par->height = codec->height;
  1751. break;
  1752. }
  1753. if (codec->extradata) {
  1754. par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1755. if (!par->extradata)
  1756. return AVERROR(ENOMEM);
  1757. memcpy(par->extradata, codec->extradata, codec->extradata_size);
  1758. par->extradata_size = codec->extradata_size;
  1759. }
  1760. return 0;
  1761. }
  1762. int avcodec_parameters_to_context(AVCodecContext *codec,
  1763. const AVCodecParameters *par)
  1764. {
  1765. codec->codec_type = par->codec_type;
  1766. codec->codec_id = par->codec_id;
  1767. codec->codec_tag = par->codec_tag;
  1768. codec->bit_rate = par->bit_rate;
  1769. codec->bits_per_coded_sample = par->bits_per_coded_sample;
  1770. codec->bits_per_raw_sample = par->bits_per_raw_sample;
  1771. codec->profile = par->profile;
  1772. codec->level = par->level;
  1773. switch (par->codec_type) {
  1774. case AVMEDIA_TYPE_VIDEO:
  1775. codec->pix_fmt = par->format;
  1776. codec->width = par->width;
  1777. codec->height = par->height;
  1778. codec->field_order = par->field_order;
  1779. codec->color_range = par->color_range;
  1780. codec->color_primaries = par->color_primaries;
  1781. codec->color_trc = par->color_trc;
  1782. codec->colorspace = par->color_space;
  1783. codec->chroma_sample_location = par->chroma_location;
  1784. codec->sample_aspect_ratio = par->sample_aspect_ratio;
  1785. codec->has_b_frames = par->video_delay;
  1786. break;
  1787. case AVMEDIA_TYPE_AUDIO:
  1788. codec->sample_fmt = par->format;
  1789. codec->channel_layout = par->channel_layout;
  1790. codec->channels = par->channels;
  1791. codec->sample_rate = par->sample_rate;
  1792. codec->block_align = par->block_align;
  1793. codec->frame_size = par->frame_size;
  1794. codec->delay =
  1795. codec->initial_padding = par->initial_padding;
  1796. codec->trailing_padding = par->trailing_padding;
  1797. codec->seek_preroll = par->seek_preroll;
  1798. break;
  1799. case AVMEDIA_TYPE_SUBTITLE:
  1800. codec->width = par->width;
  1801. codec->height = par->height;
  1802. break;
  1803. }
  1804. if (par->extradata) {
  1805. av_freep(&codec->extradata);
  1806. codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1807. if (!codec->extradata)
  1808. return AVERROR(ENOMEM);
  1809. memcpy(codec->extradata, par->extradata, par->extradata_size);
  1810. codec->extradata_size = par->extradata_size;
  1811. }
  1812. return 0;
  1813. }
  1814. static unsigned bcd2uint(uint8_t bcd)
  1815. {
  1816. unsigned low = bcd & 0xf;
  1817. unsigned high = bcd >> 4;
  1818. if (low > 9 || high > 9)
  1819. return 0;
  1820. return low + 10*high;
  1821. }
  1822. int ff_alloc_timecode_sei(const AVFrame *frame, AVRational rate, size_t prefix_len,
  1823. void **data, size_t *sei_size)
  1824. {
  1825. AVFrameSideData *sd = NULL;
  1826. uint8_t *sei_data;
  1827. PutBitContext pb;
  1828. uint32_t *tc;
  1829. int m;
  1830. if (frame)
  1831. sd = av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE);
  1832. if (!sd) {
  1833. *data = NULL;
  1834. return 0;
  1835. }
  1836. tc = (uint32_t*)sd->data;
  1837. m = tc[0] & 3;
  1838. *sei_size = sizeof(uint32_t) * 4;
  1839. *data = av_mallocz(*sei_size + prefix_len);
  1840. if (!*data)
  1841. return AVERROR(ENOMEM);
  1842. sei_data = (uint8_t*)*data + prefix_len;
  1843. init_put_bits(&pb, sei_data, *sei_size);
  1844. put_bits(&pb, 2, m); // num_clock_ts
  1845. for (int j = 1; j <= m; j++) {
  1846. uint32_t tcsmpte = tc[j];
  1847. unsigned hh = bcd2uint(tcsmpte & 0x3f); // 6-bit hours
  1848. unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f); // 7-bit minutes
  1849. unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f); // 7-bit seconds
  1850. unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f); // 6-bit frames
  1851. unsigned drop = tcsmpte & 1<<30 && !0; // 1-bit drop if not arbitrary bit
  1852. /* Calculate frame number of HEVC by SMPTE ST 12-1:2014 Sec 12.2 if rate > 30FPS */
  1853. if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
  1854. unsigned pc;
  1855. ff *= 2;
  1856. if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
  1857. pc = !!(tcsmpte & 1 << 7);
  1858. else
  1859. pc = !!(tcsmpte & 1 << 23);
  1860. ff = (ff + pc) & 0x7f;
  1861. }
  1862. put_bits(&pb, 1, 1); // clock_timestamp_flag
  1863. put_bits(&pb, 1, 1); // units_field_based_flag
  1864. put_bits(&pb, 5, 0); // counting_type
  1865. put_bits(&pb, 1, 1); // full_timestamp_flag
  1866. put_bits(&pb, 1, 0); // discontinuity_flag
  1867. put_bits(&pb, 1, drop);
  1868. put_bits(&pb, 9, ff);
  1869. put_bits(&pb, 6, ss);
  1870. put_bits(&pb, 6, mm);
  1871. put_bits(&pb, 5, hh);
  1872. put_bits(&pb, 5, 0);
  1873. }
  1874. flush_put_bits(&pb);
  1875. return 0;
  1876. }
  1877. int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
  1878. {
  1879. AVRational framerate = avctx->framerate;
  1880. int bits_per_coded_sample = avctx->bits_per_coded_sample;
  1881. int64_t bitrate;
  1882. if (!(framerate.num && framerate.den))
  1883. framerate = av_inv_q(avctx->time_base);
  1884. if (!(framerate.num && framerate.den))
  1885. return 0;
  1886. if (!bits_per_coded_sample) {
  1887. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1888. bits_per_coded_sample = av_get_bits_per_pixel(desc);
  1889. }
  1890. bitrate = (int64_t)bits_per_coded_sample * avctx->width * avctx->height *
  1891. framerate.num / framerate.den;
  1892. return bitrate;
  1893. }
  1894. int ff_int_from_list_or_default(void *ctx, const char * val_name, int val,
  1895. const int * array_valid_values, int default_value)
  1896. {
  1897. int i = 0, ref_val;
  1898. while (1) {
  1899. ref_val = array_valid_values[i];
  1900. if (ref_val == INT_MAX)
  1901. break;
  1902. if (val == ref_val)
  1903. return val;
  1904. i++;
  1905. }
  1906. /* val is not a valid value */
  1907. av_log(ctx, AV_LOG_DEBUG,
  1908. "%s %d are not supported. Set to default value : %d\n", val_name, val, default_value);
  1909. return default_value;
  1910. }