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.

1713 lines
52KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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/attributes.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/channel_layout.h"
  31. #include "libavutil/crc.h"
  32. #include "libavutil/frame.h"
  33. #include "libavutil/hwcontext.h"
  34. #include "libavutil/internal.h"
  35. #include "libavutil/mathematics.h"
  36. #include "libavutil/pixdesc.h"
  37. #include "libavutil/imgutils.h"
  38. #include "libavutil/samplefmt.h"
  39. #include "libavutil/dict.h"
  40. #include "avcodec.h"
  41. #include "libavutil/opt.h"
  42. #include "me_cmp.h"
  43. #include "mpegvideo.h"
  44. #include "thread.h"
  45. #include "internal.h"
  46. #include "bytestream.h"
  47. #include "version.h"
  48. #include <stdlib.h>
  49. #include <stdarg.h>
  50. #include <limits.h>
  51. #include <float.h>
  52. static int volatile entangled_thread_counter = 0;
  53. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
  54. static void *codec_mutex;
  55. static void *avformat_mutex;
  56. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  57. {
  58. void **p = ptr;
  59. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  60. av_freep(p);
  61. *size = 0;
  62. return;
  63. }
  64. av_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
  65. if (*size)
  66. memset((uint8_t *)*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  67. }
  68. /* encoder management */
  69. static AVCodec *first_avcodec = NULL;
  70. AVCodec *av_codec_next(const AVCodec *c)
  71. {
  72. if (c)
  73. return c->next;
  74. else
  75. return first_avcodec;
  76. }
  77. static av_cold void avcodec_init(void)
  78. {
  79. static int initialized = 0;
  80. if (initialized != 0)
  81. return;
  82. initialized = 1;
  83. if (CONFIG_ME_CMP)
  84. ff_me_cmp_init_static();
  85. }
  86. int av_codec_is_encoder(const AVCodec *codec)
  87. {
  88. return codec && (codec->encode_sub || codec->encode2 ||codec->send_frame);
  89. }
  90. int av_codec_is_decoder(const AVCodec *codec)
  91. {
  92. return codec && (codec->decode || codec->send_packet);
  93. }
  94. av_cold void avcodec_register(AVCodec *codec)
  95. {
  96. AVCodec **p;
  97. avcodec_init();
  98. p = &first_avcodec;
  99. while (*p)
  100. p = &(*p)->next;
  101. *p = codec;
  102. codec->next = NULL;
  103. if (codec->init_static_data)
  104. codec->init_static_data(codec);
  105. }
  106. #if FF_API_EMU_EDGE
  107. unsigned avcodec_get_edge_width(void)
  108. {
  109. return EDGE_WIDTH;
  110. }
  111. #endif
  112. #if FF_API_SET_DIMENSIONS
  113. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  114. {
  115. ff_set_dimensions(s, width, height);
  116. }
  117. #endif
  118. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  119. {
  120. int ret = av_image_check_size(width, height, 0, s);
  121. if (ret < 0)
  122. width = height = 0;
  123. s->width = s->coded_width = width;
  124. s->height = s->coded_height = height;
  125. return ret;
  126. }
  127. int ff_set_sar(AVCodecContext *avctx, AVRational sar)
  128. {
  129. int ret = av_image_check_sar(avctx->width, avctx->height, sar);
  130. if (ret < 0) {
  131. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
  132. sar.num, sar.den);
  133. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  134. return ret;
  135. } else {
  136. avctx->sample_aspect_ratio = sar;
  137. }
  138. return 0;
  139. }
  140. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  141. enum AVMatrixEncoding matrix_encoding)
  142. {
  143. AVFrameSideData *side_data;
  144. enum AVMatrixEncoding *data;
  145. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
  146. if (!side_data)
  147. side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
  148. sizeof(enum AVMatrixEncoding));
  149. if (!side_data)
  150. return AVERROR(ENOMEM);
  151. data = (enum AVMatrixEncoding*)side_data->data;
  152. *data = matrix_encoding;
  153. return 0;
  154. }
  155. #if HAVE_SIMD_ALIGN_32
  156. # define STRIDE_ALIGN 32
  157. #elif HAVE_SIMD_ALIGN_16
  158. # define STRIDE_ALIGN 16
  159. #else
  160. # define STRIDE_ALIGN 8
  161. #endif
  162. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  163. int linesize_align[AV_NUM_DATA_POINTERS])
  164. {
  165. int i;
  166. int w_align = 1;
  167. int h_align = 1;
  168. switch (s->pix_fmt) {
  169. case AV_PIX_FMT_YUV420P:
  170. case AV_PIX_FMT_YUYV422:
  171. case AV_PIX_FMT_YVYU422:
  172. case AV_PIX_FMT_UYVY422:
  173. case AV_PIX_FMT_YUV422P:
  174. case AV_PIX_FMT_YUV440P:
  175. case AV_PIX_FMT_YUV444P:
  176. case AV_PIX_FMT_GBRP:
  177. case AV_PIX_FMT_GBRAP:
  178. case AV_PIX_FMT_GRAY8:
  179. case AV_PIX_FMT_GRAY16BE:
  180. case AV_PIX_FMT_GRAY16LE:
  181. case AV_PIX_FMT_YUVJ420P:
  182. case AV_PIX_FMT_YUVJ422P:
  183. case AV_PIX_FMT_YUVJ440P:
  184. case AV_PIX_FMT_YUVJ444P:
  185. case AV_PIX_FMT_YUVA420P:
  186. case AV_PIX_FMT_YUVA422P:
  187. case AV_PIX_FMT_YUVA444P:
  188. case AV_PIX_FMT_YUV420P9LE:
  189. case AV_PIX_FMT_YUV420P9BE:
  190. case AV_PIX_FMT_YUV420P10LE:
  191. case AV_PIX_FMT_YUV420P10BE:
  192. case AV_PIX_FMT_YUV422P9LE:
  193. case AV_PIX_FMT_YUV422P9BE:
  194. case AV_PIX_FMT_YUV422P10LE:
  195. case AV_PIX_FMT_YUV422P10BE:
  196. case AV_PIX_FMT_YUVA422P10LE:
  197. case AV_PIX_FMT_YUVA422P10BE:
  198. case AV_PIX_FMT_YUV444P9LE:
  199. case AV_PIX_FMT_YUV444P9BE:
  200. case AV_PIX_FMT_YUV444P10LE:
  201. case AV_PIX_FMT_YUV444P10BE:
  202. case AV_PIX_FMT_YUVA444P10LE:
  203. case AV_PIX_FMT_YUVA444P10BE:
  204. case AV_PIX_FMT_GBRP9LE:
  205. case AV_PIX_FMT_GBRP9BE:
  206. case AV_PIX_FMT_GBRP10LE:
  207. case AV_PIX_FMT_GBRP10BE:
  208. case AV_PIX_FMT_GBRAP12LE:
  209. case AV_PIX_FMT_GBRAP12BE:
  210. w_align = 16; //FIXME assume 16 pixel per macroblock
  211. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  212. break;
  213. case AV_PIX_FMT_YUV411P:
  214. case AV_PIX_FMT_UYYVYY411:
  215. w_align = 32;
  216. h_align = 8;
  217. break;
  218. case AV_PIX_FMT_YUV410P:
  219. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  220. w_align = 64;
  221. h_align = 64;
  222. }
  223. case AV_PIX_FMT_RGB555:
  224. if (s->codec_id == AV_CODEC_ID_RPZA) {
  225. w_align = 4;
  226. h_align = 4;
  227. }
  228. case AV_PIX_FMT_PAL8:
  229. case AV_PIX_FMT_BGR8:
  230. case AV_PIX_FMT_RGB8:
  231. if (s->codec_id == AV_CODEC_ID_SMC) {
  232. w_align = 4;
  233. h_align = 4;
  234. }
  235. break;
  236. case AV_PIX_FMT_BGR24:
  237. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  238. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  239. w_align = 4;
  240. h_align = 4;
  241. }
  242. break;
  243. default:
  244. w_align = 1;
  245. h_align = 1;
  246. break;
  247. }
  248. *width = FFALIGN(*width, w_align);
  249. *height = FFALIGN(*height, h_align);
  250. if (s->codec_id == AV_CODEC_ID_H264)
  251. // some of the optimized chroma MC reads one line too much
  252. *height += 2;
  253. for (i = 0; i < 4; i++)
  254. linesize_align[i] = STRIDE_ALIGN;
  255. }
  256. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  257. {
  258. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  259. int chroma_shift = desc->log2_chroma_w;
  260. int linesize_align[AV_NUM_DATA_POINTERS];
  261. int align;
  262. avcodec_align_dimensions2(s, width, height, linesize_align);
  263. align = FFMAX(linesize_align[0], linesize_align[3]);
  264. linesize_align[1] <<= chroma_shift;
  265. linesize_align[2] <<= chroma_shift;
  266. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  267. *width = FFALIGN(*width, align);
  268. }
  269. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  270. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  271. int buf_size, int align)
  272. {
  273. int ch, planar, needed_size, ret = 0;
  274. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  275. frame->nb_samples, sample_fmt,
  276. align);
  277. if (buf_size < needed_size)
  278. return AVERROR(EINVAL);
  279. planar = av_sample_fmt_is_planar(sample_fmt);
  280. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  281. if (!(frame->extended_data = av_mallocz(nb_channels *
  282. sizeof(*frame->extended_data))))
  283. return AVERROR(ENOMEM);
  284. } else {
  285. frame->extended_data = frame->data;
  286. }
  287. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  288. buf, nb_channels, frame->nb_samples,
  289. sample_fmt, align)) < 0) {
  290. if (frame->extended_data != frame->data)
  291. av_free(frame->extended_data);
  292. return ret;
  293. }
  294. if (frame->extended_data != frame->data) {
  295. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  296. frame->data[ch] = frame->extended_data[ch];
  297. }
  298. return ret;
  299. }
  300. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  301. {
  302. int i;
  303. for (i = 0; i < count; i++) {
  304. int r = func(c, (char *)arg + i * size);
  305. if (ret)
  306. ret[i] = r;
  307. }
  308. return 0;
  309. }
  310. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  311. {
  312. int i;
  313. for (i = 0; i < count; i++) {
  314. int r = func(c, arg, i, 0);
  315. if (ret)
  316. ret[i] = r;
  317. }
  318. return 0;
  319. }
  320. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  321. {
  322. int ret = 0;
  323. AVDictionary *tmp = NULL;
  324. if (avcodec_is_open(avctx))
  325. return 0;
  326. if ((!codec && !avctx->codec)) {
  327. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  328. return AVERROR(EINVAL);
  329. }
  330. if ((codec && avctx->codec && codec != avctx->codec)) {
  331. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  332. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  333. return AVERROR(EINVAL);
  334. }
  335. if (!codec)
  336. codec = avctx->codec;
  337. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  338. return AVERROR(EINVAL);
  339. if (options)
  340. av_dict_copy(&tmp, *options, 0);
  341. /* If there is a user-supplied mutex locking routine, call it. */
  342. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
  343. if (lockmgr_cb) {
  344. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  345. return -1;
  346. }
  347. entangled_thread_counter++;
  348. if (entangled_thread_counter != 1) {
  349. av_log(avctx, AV_LOG_ERROR,
  350. "Insufficient thread locking. At least %d threads are "
  351. "calling avcodec_open2() at the same time right now.\n",
  352. entangled_thread_counter);
  353. ret = -1;
  354. goto end;
  355. }
  356. }
  357. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  358. if (!avctx->internal) {
  359. ret = AVERROR(ENOMEM);
  360. goto end;
  361. }
  362. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  363. if (!avctx->internal->pool) {
  364. ret = AVERROR(ENOMEM);
  365. goto free_and_end;
  366. }
  367. avctx->internal->to_free = av_frame_alloc();
  368. if (!avctx->internal->to_free) {
  369. ret = AVERROR(ENOMEM);
  370. goto free_and_end;
  371. }
  372. avctx->internal->buffer_frame = av_frame_alloc();
  373. if (!avctx->internal->buffer_frame) {
  374. ret = AVERROR(ENOMEM);
  375. goto free_and_end;
  376. }
  377. avctx->internal->buffer_pkt = av_packet_alloc();
  378. if (!avctx->internal->buffer_pkt) {
  379. ret = AVERROR(ENOMEM);
  380. goto free_and_end;
  381. }
  382. if (codec->priv_data_size > 0) {
  383. if (!avctx->priv_data) {
  384. avctx->priv_data = av_mallocz(codec->priv_data_size);
  385. if (!avctx->priv_data) {
  386. ret = AVERROR(ENOMEM);
  387. goto end;
  388. }
  389. if (codec->priv_class) {
  390. *(const AVClass **)avctx->priv_data = codec->priv_class;
  391. av_opt_set_defaults(avctx->priv_data);
  392. }
  393. }
  394. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  395. goto free_and_end;
  396. } else {
  397. avctx->priv_data = NULL;
  398. }
  399. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  400. goto free_and_end;
  401. if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
  402. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  403. else if (avctx->width && avctx->height)
  404. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  405. if (ret < 0)
  406. goto free_and_end;
  407. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  408. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  409. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  410. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  411. ff_set_dimensions(avctx, 0, 0);
  412. }
  413. if (avctx->width > 0 && avctx->height > 0) {
  414. if (av_image_check_sar(avctx->width, avctx->height,
  415. avctx->sample_aspect_ratio) < 0) {
  416. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  417. avctx->sample_aspect_ratio.num,
  418. avctx->sample_aspect_ratio.den);
  419. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  420. }
  421. }
  422. /* if the decoder init function was already called previously,
  423. * free the already allocated subtitle_header before overwriting it */
  424. if (av_codec_is_decoder(codec))
  425. av_freep(&avctx->subtitle_header);
  426. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  427. ret = AVERROR(EINVAL);
  428. goto free_and_end;
  429. }
  430. avctx->codec = codec;
  431. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  432. avctx->codec_id == AV_CODEC_ID_NONE) {
  433. avctx->codec_type = codec->type;
  434. avctx->codec_id = codec->id;
  435. }
  436. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  437. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  438. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  439. ret = AVERROR(EINVAL);
  440. goto free_and_end;
  441. }
  442. avctx->frame_number = 0;
  443. if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  444. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  445. ret = AVERROR_EXPERIMENTAL;
  446. goto free_and_end;
  447. }
  448. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  449. (!avctx->time_base.num || !avctx->time_base.den)) {
  450. avctx->time_base.num = 1;
  451. avctx->time_base.den = avctx->sample_rate;
  452. }
  453. if (HAVE_THREADS) {
  454. ret = ff_thread_init(avctx);
  455. if (ret < 0) {
  456. goto free_and_end;
  457. }
  458. }
  459. if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
  460. avctx->thread_count = 1;
  461. if (av_codec_is_encoder(avctx->codec)) {
  462. int i;
  463. #if FF_API_CODED_FRAME
  464. FF_DISABLE_DEPRECATION_WARNINGS
  465. avctx->coded_frame = av_frame_alloc();
  466. if (!avctx->coded_frame) {
  467. ret = AVERROR(ENOMEM);
  468. goto free_and_end;
  469. }
  470. FF_ENABLE_DEPRECATION_WARNINGS
  471. #endif
  472. if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
  473. av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
  474. ret = AVERROR(EINVAL);
  475. goto free_and_end;
  476. }
  477. if (avctx->codec->sample_fmts) {
  478. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  479. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  480. break;
  481. if (avctx->channels == 1 &&
  482. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  483. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  484. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  485. break;
  486. }
  487. }
  488. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  489. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  490. ret = AVERROR(EINVAL);
  491. goto free_and_end;
  492. }
  493. }
  494. if (avctx->codec->pix_fmts) {
  495. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  496. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  497. break;
  498. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
  499. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  500. ret = AVERROR(EINVAL);
  501. goto free_and_end;
  502. }
  503. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
  504. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
  505. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
  506. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
  507. avctx->color_range = AVCOL_RANGE_JPEG;
  508. }
  509. if (avctx->codec->supported_samplerates) {
  510. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  511. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  512. break;
  513. if (avctx->codec->supported_samplerates[i] == 0) {
  514. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  515. ret = AVERROR(EINVAL);
  516. goto free_and_end;
  517. }
  518. }
  519. if (avctx->codec->channel_layouts) {
  520. if (!avctx->channel_layout) {
  521. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  522. } else {
  523. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  524. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  525. break;
  526. if (avctx->codec->channel_layouts[i] == 0) {
  527. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  528. ret = AVERROR(EINVAL);
  529. goto free_and_end;
  530. }
  531. }
  532. }
  533. if (avctx->channel_layout && avctx->channels) {
  534. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  535. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  536. ret = AVERROR(EINVAL);
  537. goto free_and_end;
  538. }
  539. } else if (avctx->channel_layout) {
  540. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  541. }
  542. if (!avctx->rc_initial_buffer_occupancy)
  543. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  544. if (avctx->ticks_per_frame &&
  545. avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
  546. av_log(avctx, AV_LOG_ERROR,
  547. "ticks_per_frame %d too large for the timebase %d/%d.",
  548. avctx->ticks_per_frame,
  549. avctx->time_base.num,
  550. avctx->time_base.den);
  551. goto free_and_end;
  552. }
  553. if (avctx->hw_frames_ctx) {
  554. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  555. if (frames_ctx->format != avctx->pix_fmt) {
  556. av_log(avctx, AV_LOG_ERROR,
  557. "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
  558. ret = AVERROR(EINVAL);
  559. goto free_and_end;
  560. }
  561. if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
  562. avctx->sw_pix_fmt != frames_ctx->sw_format) {
  563. av_log(avctx, AV_LOG_ERROR,
  564. "Mismatching AVCodecContext.sw_pix_fmt (%s) "
  565. "and AVHWFramesContext.sw_format (%s)\n",
  566. av_get_pix_fmt_name(avctx->sw_pix_fmt),
  567. av_get_pix_fmt_name(frames_ctx->sw_format));
  568. ret = AVERROR(EINVAL);
  569. goto free_and_end;
  570. }
  571. avctx->sw_pix_fmt = frames_ctx->sw_format;
  572. }
  573. }
  574. if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  575. ret = avctx->codec->init(avctx);
  576. if (ret < 0) {
  577. goto free_and_end;
  578. }
  579. }
  580. #if FF_API_AUDIOENC_DELAY
  581. if (av_codec_is_encoder(avctx->codec))
  582. avctx->delay = avctx->initial_padding;
  583. #endif
  584. if (av_codec_is_decoder(avctx->codec)) {
  585. /* validate channel layout from the decoder */
  586. if (avctx->channel_layout) {
  587. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  588. if (!avctx->channels)
  589. avctx->channels = channels;
  590. else if (channels != avctx->channels) {
  591. av_log(avctx, AV_LOG_WARNING,
  592. "channel layout does not match number of channels\n");
  593. avctx->channel_layout = 0;
  594. }
  595. }
  596. if (avctx->channels && avctx->channels < 0 ||
  597. avctx->channels > FF_SANE_NB_CHANNELS) {
  598. ret = AVERROR(EINVAL);
  599. goto free_and_end;
  600. }
  601. #if FF_API_AVCTX_TIMEBASE
  602. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  603. avctx->time_base = av_inv_q(avctx->framerate);
  604. #endif
  605. }
  606. end:
  607. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
  608. entangled_thread_counter--;
  609. /* Release any user-supplied mutex. */
  610. if (lockmgr_cb) {
  611. (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  612. }
  613. }
  614. if (options) {
  615. av_dict_free(options);
  616. *options = tmp;
  617. }
  618. return ret;
  619. free_and_end:
  620. if (avctx->codec &&
  621. (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
  622. avctx->codec->close(avctx);
  623. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  624. av_opt_free(avctx->priv_data);
  625. av_opt_free(avctx);
  626. #if FF_API_CODED_FRAME
  627. FF_DISABLE_DEPRECATION_WARNINGS
  628. av_frame_free(&avctx->coded_frame);
  629. FF_ENABLE_DEPRECATION_WARNINGS
  630. #endif
  631. av_dict_free(&tmp);
  632. av_freep(&avctx->priv_data);
  633. if (avctx->internal) {
  634. av_frame_free(&avctx->internal->to_free);
  635. av_frame_free(&avctx->internal->buffer_frame);
  636. av_packet_free(&avctx->internal->buffer_pkt);
  637. av_freep(&avctx->internal->pool);
  638. }
  639. av_freep(&avctx->internal);
  640. avctx->codec = NULL;
  641. goto end;
  642. }
  643. void avsubtitle_free(AVSubtitle *sub)
  644. {
  645. int i;
  646. for (i = 0; i < sub->num_rects; i++) {
  647. av_freep(&sub->rects[i]->data[0]);
  648. av_freep(&sub->rects[i]->data[1]);
  649. av_freep(&sub->rects[i]->data[2]);
  650. av_freep(&sub->rects[i]->data[3]);
  651. av_freep(&sub->rects[i]->text);
  652. av_freep(&sub->rects[i]->ass);
  653. av_freep(&sub->rects[i]);
  654. }
  655. av_freep(&sub->rects);
  656. memset(sub, 0, sizeof(AVSubtitle));
  657. }
  658. av_cold int avcodec_close(AVCodecContext *avctx)
  659. {
  660. int i;
  661. if (avcodec_is_open(avctx)) {
  662. FramePool *pool = avctx->internal->pool;
  663. if (HAVE_THREADS && avctx->internal->thread_ctx)
  664. ff_thread_free(avctx);
  665. if (avctx->codec && avctx->codec->close)
  666. avctx->codec->close(avctx);
  667. av_frame_free(&avctx->internal->to_free);
  668. av_frame_free(&avctx->internal->buffer_frame);
  669. av_packet_free(&avctx->internal->buffer_pkt);
  670. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  671. av_buffer_pool_uninit(&pool->pools[i]);
  672. av_freep(&avctx->internal->pool);
  673. if (avctx->hwaccel && avctx->hwaccel->uninit)
  674. avctx->hwaccel->uninit(avctx);
  675. av_freep(&avctx->internal->hwaccel_priv_data);
  676. av_freep(&avctx->internal);
  677. }
  678. for (i = 0; i < avctx->nb_coded_side_data; i++)
  679. av_freep(&avctx->coded_side_data[i].data);
  680. av_freep(&avctx->coded_side_data);
  681. avctx->nb_coded_side_data = 0;
  682. av_buffer_unref(&avctx->hw_frames_ctx);
  683. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  684. av_opt_free(avctx->priv_data);
  685. av_opt_free(avctx);
  686. av_freep(&avctx->priv_data);
  687. if (av_codec_is_encoder(avctx->codec)) {
  688. av_freep(&avctx->extradata);
  689. #if FF_API_CODED_FRAME
  690. FF_DISABLE_DEPRECATION_WARNINGS
  691. av_frame_free(&avctx->coded_frame);
  692. FF_ENABLE_DEPRECATION_WARNINGS
  693. #endif
  694. }
  695. avctx->codec = NULL;
  696. avctx->active_thread_type = 0;
  697. return 0;
  698. }
  699. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  700. {
  701. AVCodec *p, *experimental = NULL;
  702. p = first_avcodec;
  703. while (p) {
  704. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  705. p->id == id) {
  706. if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
  707. experimental = p;
  708. } else
  709. return p;
  710. }
  711. p = p->next;
  712. }
  713. return experimental;
  714. }
  715. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  716. {
  717. return find_encdec(id, 1);
  718. }
  719. AVCodec *avcodec_find_encoder_by_name(const char *name)
  720. {
  721. AVCodec *p;
  722. if (!name)
  723. return NULL;
  724. p = first_avcodec;
  725. while (p) {
  726. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  727. return p;
  728. p = p->next;
  729. }
  730. return NULL;
  731. }
  732. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  733. {
  734. return find_encdec(id, 0);
  735. }
  736. AVCodec *avcodec_find_decoder_by_name(const char *name)
  737. {
  738. AVCodec *p;
  739. if (!name)
  740. return NULL;
  741. p = first_avcodec;
  742. while (p) {
  743. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  744. return p;
  745. p = p->next;
  746. }
  747. return NULL;
  748. }
  749. static int get_bit_rate(AVCodecContext *ctx)
  750. {
  751. int bit_rate;
  752. int bits_per_sample;
  753. switch (ctx->codec_type) {
  754. case AVMEDIA_TYPE_VIDEO:
  755. case AVMEDIA_TYPE_DATA:
  756. case AVMEDIA_TYPE_SUBTITLE:
  757. case AVMEDIA_TYPE_ATTACHMENT:
  758. bit_rate = ctx->bit_rate;
  759. break;
  760. case AVMEDIA_TYPE_AUDIO:
  761. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  762. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  763. break;
  764. default:
  765. bit_rate = 0;
  766. break;
  767. }
  768. return bit_rate;
  769. }
  770. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  771. {
  772. int i, len, ret = 0;
  773. #define TAG_PRINT(x) \
  774. (((x) >= '0' && (x) <= '9') || \
  775. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  776. ((x) == '.' || (x) == ' '))
  777. for (i = 0; i < 4; i++) {
  778. len = snprintf(buf, buf_size,
  779. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  780. buf += len;
  781. buf_size = buf_size > len ? buf_size - len : 0;
  782. ret += len;
  783. codec_tag >>= 8;
  784. }
  785. return ret;
  786. }
  787. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  788. {
  789. const char *codec_name;
  790. const char *profile = NULL;
  791. char buf1[32];
  792. int bitrate;
  793. int new_line = 0;
  794. AVRational display_aspect_ratio;
  795. const AVCodecDescriptor *desc = avcodec_descriptor_get(enc->codec_id);
  796. if (desc) {
  797. codec_name = desc->name;
  798. profile = avcodec_profile_name(enc->codec_id, enc->profile);
  799. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  800. /* fake mpeg2 transport stream codec (currently not
  801. * registered) */
  802. codec_name = "mpeg2ts";
  803. } else {
  804. /* output avi tags */
  805. char tag_buf[32];
  806. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  807. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  808. codec_name = buf1;
  809. }
  810. switch (enc->codec_type) {
  811. case AVMEDIA_TYPE_VIDEO:
  812. snprintf(buf, buf_size,
  813. "Video: %s%s",
  814. codec_name, enc->mb_decision ? " (hq)" : "");
  815. if (profile)
  816. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  817. " (%s)", profile);
  818. if (enc->codec_tag) {
  819. char tag_buf[32];
  820. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  821. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  822. " [%s / 0x%04X]", tag_buf, enc->codec_tag);
  823. }
  824. av_strlcat(buf, "\n ", buf_size);
  825. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  826. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  827. av_get_pix_fmt_name(enc->pix_fmt));
  828. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  829. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
  830. av_color_range_name(enc->color_range));
  831. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  832. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  833. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  834. new_line = 1;
  835. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s/%s/%s",
  836. av_color_space_name(enc->colorspace),
  837. av_color_primaries_name(enc->color_primaries),
  838. av_color_transfer_name(enc->color_trc));
  839. }
  840. if (av_log_get_level() >= AV_LOG_DEBUG &&
  841. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  842. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
  843. av_chroma_location_name(enc->chroma_sample_location));
  844. if (enc->width) {
  845. av_strlcat(buf, new_line ? "\n " : ", ", buf_size);
  846. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  847. "%dx%d",
  848. enc->width, enc->height);
  849. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  850. (enc->width != enc->coded_width ||
  851. enc->height != enc->coded_height))
  852. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  853. " (%dx%d)", enc->coded_width, enc->coded_height);
  854. if (enc->sample_aspect_ratio.num) {
  855. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  856. enc->width * enc->sample_aspect_ratio.num,
  857. enc->height * enc->sample_aspect_ratio.den,
  858. 1024 * 1024);
  859. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  860. " [PAR %d:%d DAR %d:%d]",
  861. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  862. display_aspect_ratio.num, display_aspect_ratio.den);
  863. }
  864. if (av_log_get_level() >= AV_LOG_DEBUG) {
  865. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  866. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  867. ", %d/%d",
  868. enc->time_base.num / g, enc->time_base.den / g);
  869. }
  870. }
  871. if (encode) {
  872. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  873. ", q=%d-%d", enc->qmin, enc->qmax);
  874. }
  875. break;
  876. case AVMEDIA_TYPE_AUDIO:
  877. snprintf(buf, buf_size,
  878. "Audio: %s",
  879. codec_name);
  880. if (profile)
  881. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  882. " (%s)", profile);
  883. if (enc->codec_tag) {
  884. char tag_buf[32];
  885. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  886. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  887. " [%s / 0x%04X]", tag_buf, enc->codec_tag);
  888. }
  889. av_strlcat(buf, "\n ", buf_size);
  890. if (enc->sample_rate) {
  891. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  892. "%d Hz, ", enc->sample_rate);
  893. }
  894. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  895. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  896. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  897. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  898. }
  899. break;
  900. case AVMEDIA_TYPE_DATA:
  901. snprintf(buf, buf_size, "Data: %s", codec_name);
  902. break;
  903. case AVMEDIA_TYPE_SUBTITLE:
  904. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  905. break;
  906. case AVMEDIA_TYPE_ATTACHMENT:
  907. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  908. break;
  909. default:
  910. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  911. return;
  912. }
  913. if (encode) {
  914. if (enc->flags & AV_CODEC_FLAG_PASS1)
  915. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  916. ", pass 1");
  917. if (enc->flags & AV_CODEC_FLAG_PASS2)
  918. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  919. ", pass 2");
  920. }
  921. bitrate = get_bit_rate(enc);
  922. if (bitrate != 0) {
  923. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  924. ", %d kb/s", bitrate / 1000);
  925. }
  926. }
  927. const char *av_get_profile_name(const AVCodec *codec, int profile)
  928. {
  929. const AVProfile *p;
  930. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  931. return NULL;
  932. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  933. if (p->profile == profile)
  934. return p->name;
  935. return NULL;
  936. }
  937. const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
  938. {
  939. const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
  940. const AVProfile *p;
  941. if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
  942. return NULL;
  943. for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  944. if (p->profile == profile)
  945. return p->name;
  946. return NULL;
  947. }
  948. unsigned avcodec_version(void)
  949. {
  950. return LIBAVCODEC_VERSION_INT;
  951. }
  952. const char *avcodec_configuration(void)
  953. {
  954. return LIBAV_CONFIGURATION;
  955. }
  956. const char *avcodec_license(void)
  957. {
  958. #define LICENSE_PREFIX "libavcodec license: "
  959. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  960. }
  961. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  962. {
  963. switch (codec_id) {
  964. case AV_CODEC_ID_ADPCM_CT:
  965. case AV_CODEC_ID_ADPCM_IMA_APC:
  966. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  967. case AV_CODEC_ID_ADPCM_IMA_WS:
  968. case AV_CODEC_ID_ADPCM_G722:
  969. case AV_CODEC_ID_ADPCM_YAMAHA:
  970. return 4;
  971. case AV_CODEC_ID_PCM_ALAW:
  972. case AV_CODEC_ID_PCM_MULAW:
  973. case AV_CODEC_ID_PCM_S8:
  974. case AV_CODEC_ID_PCM_U8:
  975. case AV_CODEC_ID_PCM_ZORK:
  976. return 8;
  977. case AV_CODEC_ID_PCM_S16BE:
  978. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  979. case AV_CODEC_ID_PCM_S16LE:
  980. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  981. case AV_CODEC_ID_PCM_U16BE:
  982. case AV_CODEC_ID_PCM_U16LE:
  983. return 16;
  984. case AV_CODEC_ID_PCM_S24DAUD:
  985. case AV_CODEC_ID_PCM_S24BE:
  986. case AV_CODEC_ID_PCM_S24LE:
  987. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  988. case AV_CODEC_ID_PCM_U24BE:
  989. case AV_CODEC_ID_PCM_U24LE:
  990. return 24;
  991. case AV_CODEC_ID_PCM_S32BE:
  992. case AV_CODEC_ID_PCM_S32LE:
  993. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  994. case AV_CODEC_ID_PCM_U32BE:
  995. case AV_CODEC_ID_PCM_U32LE:
  996. case AV_CODEC_ID_PCM_F32BE:
  997. case AV_CODEC_ID_PCM_F32LE:
  998. return 32;
  999. case AV_CODEC_ID_PCM_F64BE:
  1000. case AV_CODEC_ID_PCM_F64LE:
  1001. return 64;
  1002. default:
  1003. return 0;
  1004. }
  1005. }
  1006. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1007. {
  1008. switch (codec_id) {
  1009. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1010. return 2;
  1011. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1012. return 3;
  1013. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1014. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1015. case AV_CODEC_ID_ADPCM_IMA_QT:
  1016. case AV_CODEC_ID_ADPCM_SWF:
  1017. case AV_CODEC_ID_ADPCM_MS:
  1018. return 4;
  1019. default:
  1020. return av_get_exact_bits_per_sample(codec_id);
  1021. }
  1022. }
  1023. static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
  1024. uint32_t tag, int bits_per_coded_sample, int frame_bytes)
  1025. {
  1026. int bps = av_get_exact_bits_per_sample(id);
  1027. /* codecs with an exact constant bits per sample */
  1028. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1029. return (frame_bytes * 8) / (bps * ch);
  1030. bps = bits_per_coded_sample;
  1031. /* codecs with a fixed packet duration */
  1032. switch (id) {
  1033. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1034. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1035. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1036. case AV_CODEC_ID_AMR_NB:
  1037. case AV_CODEC_ID_GSM:
  1038. case AV_CODEC_ID_QCELP:
  1039. case AV_CODEC_ID_RA_144:
  1040. case AV_CODEC_ID_RA_288: return 160;
  1041. case AV_CODEC_ID_IMC: return 256;
  1042. case AV_CODEC_ID_AMR_WB:
  1043. case AV_CODEC_ID_GSM_MS: return 320;
  1044. case AV_CODEC_ID_MP1: return 384;
  1045. case AV_CODEC_ID_ATRAC1: return 512;
  1046. case AV_CODEC_ID_ATRAC3: return 1024;
  1047. case AV_CODEC_ID_MP2:
  1048. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1049. case AV_CODEC_ID_AC3: return 1536;
  1050. }
  1051. if (sr > 0) {
  1052. /* calc from sample rate */
  1053. if (id == AV_CODEC_ID_TTA)
  1054. return 256 * sr / 245;
  1055. if (ch > 0) {
  1056. /* calc from sample rate and channels */
  1057. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1058. return (480 << (sr / 22050)) / ch;
  1059. }
  1060. }
  1061. if (ba > 0) {
  1062. /* calc from block_align */
  1063. if (id == AV_CODEC_ID_SIPR) {
  1064. switch (ba) {
  1065. case 20: return 160;
  1066. case 19: return 144;
  1067. case 29: return 288;
  1068. case 37: return 480;
  1069. }
  1070. } else if (id == AV_CODEC_ID_ILBC) {
  1071. switch (ba) {
  1072. case 38: return 160;
  1073. case 50: return 240;
  1074. }
  1075. }
  1076. }
  1077. if (frame_bytes > 0) {
  1078. /* calc from frame_bytes only */
  1079. if (id == AV_CODEC_ID_TRUESPEECH)
  1080. return 240 * (frame_bytes / 32);
  1081. if (id == AV_CODEC_ID_NELLYMOSER)
  1082. return 256 * (frame_bytes / 64);
  1083. if (bps > 0) {
  1084. /* calc from frame_bytes and bits_per_coded_sample */
  1085. if (id == AV_CODEC_ID_ADPCM_G726)
  1086. return frame_bytes * 8 / bps;
  1087. }
  1088. if (ch > 0) {
  1089. /* calc from frame_bytes and channels */
  1090. switch (id) {
  1091. case AV_CODEC_ID_ADPCM_4XM:
  1092. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1093. return (frame_bytes - 4 * ch) * 2 / ch;
  1094. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1095. return (frame_bytes - 4) * 2 / ch;
  1096. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1097. return (frame_bytes - 8) * 2 / ch;
  1098. case AV_CODEC_ID_ADPCM_XA:
  1099. return (frame_bytes / 128) * 224 / ch;
  1100. case AV_CODEC_ID_INTERPLAY_DPCM:
  1101. return (frame_bytes - 6 - ch) / ch;
  1102. case AV_CODEC_ID_ROQ_DPCM:
  1103. return (frame_bytes - 8) / ch;
  1104. case AV_CODEC_ID_XAN_DPCM:
  1105. return (frame_bytes - 2 * ch) / ch;
  1106. case AV_CODEC_ID_MACE3:
  1107. return 3 * frame_bytes / ch;
  1108. case AV_CODEC_ID_MACE6:
  1109. return 6 * frame_bytes / ch;
  1110. case AV_CODEC_ID_PCM_LXF:
  1111. return 2 * (frame_bytes / (5 * ch));
  1112. }
  1113. if (tag) {
  1114. /* calc from frame_bytes, channels, and codec_tag */
  1115. if (id == AV_CODEC_ID_SOL_DPCM) {
  1116. if (tag == 3)
  1117. return frame_bytes / ch;
  1118. else
  1119. return frame_bytes * 2 / ch;
  1120. }
  1121. }
  1122. if (ba > 0) {
  1123. /* calc from frame_bytes, channels, and block_align */
  1124. int blocks = frame_bytes / ba;
  1125. switch (id) {
  1126. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1127. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1128. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1129. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1130. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1131. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1132. case AV_CODEC_ID_ADPCM_MS:
  1133. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1134. }
  1135. }
  1136. if (bps > 0) {
  1137. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1138. switch (id) {
  1139. case AV_CODEC_ID_PCM_DVD:
  1140. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1141. case AV_CODEC_ID_PCM_BLURAY:
  1142. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1143. case AV_CODEC_ID_S302M:
  1144. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1145. }
  1146. }
  1147. }
  1148. }
  1149. return 0;
  1150. }
  1151. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1152. {
  1153. return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
  1154. avctx->channels, avctx->block_align,
  1155. avctx->codec_tag, avctx->bits_per_coded_sample,
  1156. frame_bytes);
  1157. }
  1158. int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
  1159. {
  1160. return get_audio_frame_duration(par->codec_id, par->sample_rate,
  1161. par->channels, par->block_align,
  1162. par->codec_tag, par->bits_per_coded_sample,
  1163. frame_bytes);
  1164. }
  1165. #if !HAVE_THREADS
  1166. int ff_thread_init(AVCodecContext *s)
  1167. {
  1168. return -1;
  1169. }
  1170. #endif
  1171. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1172. {
  1173. unsigned int n = 0;
  1174. while (v >= 0xff) {
  1175. *s++ = 0xff;
  1176. v -= 0xff;
  1177. n++;
  1178. }
  1179. *s = v;
  1180. n++;
  1181. return n;
  1182. }
  1183. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1184. {
  1185. int i;
  1186. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1187. return i;
  1188. }
  1189. #if FF_API_MISSING_SAMPLE
  1190. FF_DISABLE_DEPRECATION_WARNINGS
  1191. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1192. {
  1193. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
  1194. "version to the newest one from Git. If the problem still "
  1195. "occurs, it means that your file has a feature which has not "
  1196. "been implemented.\n", feature);
  1197. if(want_sample)
  1198. av_log_ask_for_sample(avc, NULL);
  1199. }
  1200. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1201. {
  1202. va_list argument_list;
  1203. va_start(argument_list, msg);
  1204. if (msg)
  1205. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1206. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1207. "of this file to ftp://upload.libav.org/incoming/ "
  1208. "and contact the libav-devel mailing list.\n");
  1209. va_end(argument_list);
  1210. }
  1211. FF_ENABLE_DEPRECATION_WARNINGS
  1212. #endif /* FF_API_MISSING_SAMPLE */
  1213. static AVHWAccel *first_hwaccel = NULL;
  1214. void av_register_hwaccel(AVHWAccel *hwaccel)
  1215. {
  1216. AVHWAccel **p = &first_hwaccel;
  1217. while (*p)
  1218. p = &(*p)->next;
  1219. *p = hwaccel;
  1220. hwaccel->next = NULL;
  1221. }
  1222. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  1223. {
  1224. return hwaccel ? hwaccel->next : first_hwaccel;
  1225. }
  1226. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1227. {
  1228. if (lockmgr_cb) {
  1229. // There is no good way to rollback a failure to destroy the
  1230. // mutex, so we ignore failures.
  1231. lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
  1232. lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
  1233. lockmgr_cb = NULL;
  1234. codec_mutex = NULL;
  1235. avformat_mutex = NULL;
  1236. }
  1237. if (cb) {
  1238. void *new_codec_mutex = NULL;
  1239. void *new_avformat_mutex = NULL;
  1240. int err;
  1241. if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
  1242. return err > 0 ? AVERROR_UNKNOWN : err;
  1243. }
  1244. if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
  1245. // Ignore failures to destroy the newly created mutex.
  1246. cb(&new_codec_mutex, AV_LOCK_DESTROY);
  1247. return err > 0 ? AVERROR_UNKNOWN : err;
  1248. }
  1249. lockmgr_cb = cb;
  1250. codec_mutex = new_codec_mutex;
  1251. avformat_mutex = new_avformat_mutex;
  1252. }
  1253. return 0;
  1254. }
  1255. int avpriv_lock_avformat(void)
  1256. {
  1257. if (lockmgr_cb) {
  1258. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1259. return -1;
  1260. }
  1261. return 0;
  1262. }
  1263. int avpriv_unlock_avformat(void)
  1264. {
  1265. if (lockmgr_cb) {
  1266. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1267. return -1;
  1268. }
  1269. return 0;
  1270. }
  1271. unsigned int avpriv_toupper4(unsigned int x)
  1272. {
  1273. return av_toupper(x & 0xFF) +
  1274. (av_toupper((x >> 8) & 0xFF) << 8) +
  1275. (av_toupper((x >> 16) & 0xFF) << 16) +
  1276. (av_toupper((x >> 24) & 0xFF) << 24);
  1277. }
  1278. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  1279. {
  1280. int ret;
  1281. dst->owner = src->owner;
  1282. ret = av_frame_ref(dst->f, src->f);
  1283. if (ret < 0)
  1284. return ret;
  1285. if (src->progress &&
  1286. !(dst->progress = av_buffer_ref(src->progress))) {
  1287. ff_thread_release_buffer(dst->owner, dst);
  1288. return AVERROR(ENOMEM);
  1289. }
  1290. return 0;
  1291. }
  1292. #if !HAVE_THREADS
  1293. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  1294. {
  1295. f->owner = avctx;
  1296. return ff_get_buffer(avctx, f->f, flags);
  1297. }
  1298. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  1299. {
  1300. if (f->f)
  1301. av_frame_unref(f->f);
  1302. }
  1303. void ff_thread_finish_setup(AVCodecContext *avctx)
  1304. {
  1305. }
  1306. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  1307. {
  1308. }
  1309. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  1310. {
  1311. }
  1312. #endif
  1313. int avcodec_is_open(AVCodecContext *s)
  1314. {
  1315. return !!s->internal;
  1316. }
  1317. const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
  1318. const uint8_t *end,
  1319. uint32_t * restrict state)
  1320. {
  1321. int i;
  1322. assert(p <= end);
  1323. if (p >= end)
  1324. return end;
  1325. for (i = 0; i < 3; i++) {
  1326. uint32_t tmp = *state << 8;
  1327. *state = tmp + *(p++);
  1328. if (tmp == 0x100 || p == end)
  1329. return p;
  1330. }
  1331. while (p < end) {
  1332. if (p[-1] > 1 ) p += 3;
  1333. else if (p[-2] ) p += 2;
  1334. else if (p[-3]|(p[-1]-1)) p++;
  1335. else {
  1336. p++;
  1337. break;
  1338. }
  1339. }
  1340. p = FFMIN(p, end) - 4;
  1341. *state = AV_RB32(p);
  1342. return p + 4;
  1343. }
  1344. AVCPBProperties *av_cpb_properties_alloc(size_t *size)
  1345. {
  1346. AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
  1347. if (!props)
  1348. return NULL;
  1349. if (size)
  1350. *size = sizeof(*props);
  1351. props->vbv_delay = UINT64_MAX;
  1352. return props;
  1353. }
  1354. AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
  1355. {
  1356. AVPacketSideData *tmp;
  1357. AVCPBProperties *props;
  1358. size_t size;
  1359. props = av_cpb_properties_alloc(&size);
  1360. if (!props)
  1361. return NULL;
  1362. tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
  1363. if (!tmp) {
  1364. av_freep(&props);
  1365. return NULL;
  1366. }
  1367. avctx->coded_side_data = tmp;
  1368. avctx->nb_coded_side_data++;
  1369. avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
  1370. avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
  1371. avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
  1372. return props;
  1373. }
  1374. static void codec_parameters_reset(AVCodecParameters *par)
  1375. {
  1376. av_freep(&par->extradata);
  1377. memset(par, 0, sizeof(*par));
  1378. par->codec_type = AVMEDIA_TYPE_UNKNOWN;
  1379. par->codec_id = AV_CODEC_ID_NONE;
  1380. par->format = -1;
  1381. par->field_order = AV_FIELD_UNKNOWN;
  1382. par->color_range = AVCOL_RANGE_UNSPECIFIED;
  1383. par->color_primaries = AVCOL_PRI_UNSPECIFIED;
  1384. par->color_trc = AVCOL_TRC_UNSPECIFIED;
  1385. par->color_space = AVCOL_SPC_UNSPECIFIED;
  1386. par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  1387. par->sample_aspect_ratio = (AVRational){ 0, 1 };
  1388. }
  1389. AVCodecParameters *avcodec_parameters_alloc(void)
  1390. {
  1391. AVCodecParameters *par = av_mallocz(sizeof(*par));
  1392. if (!par)
  1393. return NULL;
  1394. codec_parameters_reset(par);
  1395. return par;
  1396. }
  1397. void avcodec_parameters_free(AVCodecParameters **ppar)
  1398. {
  1399. AVCodecParameters *par = *ppar;
  1400. if (!par)
  1401. return;
  1402. codec_parameters_reset(par);
  1403. av_freep(ppar);
  1404. }
  1405. int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
  1406. {
  1407. codec_parameters_reset(dst);
  1408. memcpy(dst, src, sizeof(*dst));
  1409. dst->extradata = NULL;
  1410. dst->extradata_size = 0;
  1411. if (src->extradata) {
  1412. dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1413. if (!dst->extradata)
  1414. return AVERROR(ENOMEM);
  1415. memcpy(dst->extradata, src->extradata, src->extradata_size);
  1416. dst->extradata_size = src->extradata_size;
  1417. }
  1418. return 0;
  1419. }
  1420. int avcodec_parameters_from_context(AVCodecParameters *par,
  1421. const AVCodecContext *codec)
  1422. {
  1423. codec_parameters_reset(par);
  1424. par->codec_type = codec->codec_type;
  1425. par->codec_id = codec->codec_id;
  1426. par->codec_tag = codec->codec_tag;
  1427. par->bit_rate = codec->bit_rate;
  1428. par->bits_per_coded_sample = codec->bits_per_coded_sample;
  1429. par->profile = codec->profile;
  1430. par->level = codec->level;
  1431. switch (par->codec_type) {
  1432. case AVMEDIA_TYPE_VIDEO:
  1433. par->format = codec->pix_fmt;
  1434. par->width = codec->width;
  1435. par->height = codec->height;
  1436. par->field_order = codec->field_order;
  1437. par->color_range = codec->color_range;
  1438. par->color_primaries = codec->color_primaries;
  1439. par->color_trc = codec->color_trc;
  1440. par->color_space = codec->colorspace;
  1441. par->chroma_location = codec->chroma_sample_location;
  1442. par->sample_aspect_ratio = codec->sample_aspect_ratio;
  1443. break;
  1444. case AVMEDIA_TYPE_AUDIO:
  1445. par->format = codec->sample_fmt;
  1446. par->channel_layout = codec->channel_layout;
  1447. par->channels = codec->channels;
  1448. par->sample_rate = codec->sample_rate;
  1449. par->block_align = codec->block_align;
  1450. par->initial_padding = codec->initial_padding;
  1451. break;
  1452. }
  1453. if (codec->extradata) {
  1454. par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1455. if (!par->extradata)
  1456. return AVERROR(ENOMEM);
  1457. memcpy(par->extradata, codec->extradata, codec->extradata_size);
  1458. par->extradata_size = codec->extradata_size;
  1459. }
  1460. return 0;
  1461. }
  1462. int avcodec_parameters_to_context(AVCodecContext *codec,
  1463. const AVCodecParameters *par)
  1464. {
  1465. codec->codec_type = par->codec_type;
  1466. codec->codec_id = par->codec_id;
  1467. codec->codec_tag = par->codec_tag;
  1468. codec->bit_rate = par->bit_rate;
  1469. codec->bits_per_coded_sample = par->bits_per_coded_sample;
  1470. codec->profile = par->profile;
  1471. codec->level = par->level;
  1472. switch (par->codec_type) {
  1473. case AVMEDIA_TYPE_VIDEO:
  1474. codec->pix_fmt = par->format;
  1475. codec->width = par->width;
  1476. codec->height = par->height;
  1477. codec->field_order = par->field_order;
  1478. codec->color_range = par->color_range;
  1479. codec->color_primaries = par->color_primaries;
  1480. codec->color_trc = par->color_trc;
  1481. codec->colorspace = par->color_space;
  1482. codec->chroma_sample_location = par->chroma_location;
  1483. codec->sample_aspect_ratio = par->sample_aspect_ratio;
  1484. break;
  1485. case AVMEDIA_TYPE_AUDIO:
  1486. codec->sample_fmt = par->format;
  1487. codec->channel_layout = par->channel_layout;
  1488. codec->channels = par->channels;
  1489. codec->sample_rate = par->sample_rate;
  1490. codec->block_align = par->block_align;
  1491. codec->initial_padding = par->initial_padding;
  1492. break;
  1493. }
  1494. if (par->extradata) {
  1495. av_freep(&codec->extradata);
  1496. codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1497. if (!codec->extradata)
  1498. return AVERROR(ENOMEM);
  1499. memcpy(codec->extradata, par->extradata, par->extradata_size);
  1500. codec->extradata_size = par->extradata_size;
  1501. }
  1502. return 0;
  1503. }