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.

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