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.

2367 lines
80KB

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