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.

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