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.

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