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.

2420 lines
79KB

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