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.

2440 lines
80KB

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