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.

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