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.

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