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.

2338 lines
79KB

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