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.

2477 lines
81KB

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