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.

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