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.

2432 lines
80KB

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