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.

2323 lines
78KB

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