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.

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