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.

1693 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. #if FF_API_AVCTX_TIMEBASE
  596. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  597. avctx->time_base = av_inv_q(avctx->framerate);
  598. #endif
  599. }
  600. end:
  601. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
  602. entangled_thread_counter--;
  603. /* Release any user-supplied mutex. */
  604. if (lockmgr_cb) {
  605. (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  606. }
  607. }
  608. if (options) {
  609. av_dict_free(options);
  610. *options = tmp;
  611. }
  612. return ret;
  613. free_and_end:
  614. if (avctx->codec &&
  615. (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
  616. avctx->codec->close(avctx);
  617. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  618. av_opt_free(avctx->priv_data);
  619. av_opt_free(avctx);
  620. #if FF_API_CODED_FRAME
  621. FF_DISABLE_DEPRECATION_WARNINGS
  622. av_frame_free(&avctx->coded_frame);
  623. FF_ENABLE_DEPRECATION_WARNINGS
  624. #endif
  625. av_dict_free(&tmp);
  626. av_freep(&avctx->priv_data);
  627. if (avctx->internal) {
  628. av_frame_free(&avctx->internal->to_free);
  629. av_frame_free(&avctx->internal->compat_decode_frame);
  630. av_frame_free(&avctx->internal->buffer_frame);
  631. av_packet_free(&avctx->internal->buffer_pkt);
  632. av_packet_free(&avctx->internal->last_pkt_props);
  633. av_packet_free(&avctx->internal->ds.in_pkt);
  634. av_freep(&avctx->internal->pool);
  635. }
  636. av_freep(&avctx->internal);
  637. avctx->codec = NULL;
  638. goto end;
  639. }
  640. void avsubtitle_free(AVSubtitle *sub)
  641. {
  642. int i;
  643. for (i = 0; i < sub->num_rects; i++) {
  644. av_freep(&sub->rects[i]->data[0]);
  645. av_freep(&sub->rects[i]->data[1]);
  646. av_freep(&sub->rects[i]->data[2]);
  647. av_freep(&sub->rects[i]->data[3]);
  648. av_freep(&sub->rects[i]->text);
  649. av_freep(&sub->rects[i]->ass);
  650. av_freep(&sub->rects[i]);
  651. }
  652. av_freep(&sub->rects);
  653. memset(sub, 0, sizeof(AVSubtitle));
  654. }
  655. av_cold int avcodec_close(AVCodecContext *avctx)
  656. {
  657. int i;
  658. if (avcodec_is_open(avctx)) {
  659. FramePool *pool = avctx->internal->pool;
  660. if (HAVE_THREADS && avctx->internal->thread_ctx)
  661. ff_thread_free(avctx);
  662. if (avctx->codec && avctx->codec->close)
  663. avctx->codec->close(avctx);
  664. av_frame_free(&avctx->internal->to_free);
  665. av_frame_free(&avctx->internal->compat_decode_frame);
  666. av_frame_free(&avctx->internal->buffer_frame);
  667. av_packet_free(&avctx->internal->buffer_pkt);
  668. av_packet_free(&avctx->internal->last_pkt_props);
  669. av_packet_free(&avctx->internal->ds.in_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. ff_decode_bsfs_uninit(avctx);
  677. av_freep(&avctx->internal);
  678. }
  679. for (i = 0; i < avctx->nb_coded_side_data; i++)
  680. av_freep(&avctx->coded_side_data[i].data);
  681. av_freep(&avctx->coded_side_data);
  682. avctx->nb_coded_side_data = 0;
  683. av_buffer_unref(&avctx->hw_frames_ctx);
  684. av_buffer_unref(&avctx->hw_device_ctx);
  685. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  686. av_opt_free(avctx->priv_data);
  687. av_opt_free(avctx);
  688. av_freep(&avctx->priv_data);
  689. if (av_codec_is_encoder(avctx->codec)) {
  690. av_freep(&avctx->extradata);
  691. #if FF_API_CODED_FRAME
  692. FF_DISABLE_DEPRECATION_WARNINGS
  693. av_frame_free(&avctx->coded_frame);
  694. FF_ENABLE_DEPRECATION_WARNINGS
  695. #endif
  696. }
  697. avctx->codec = NULL;
  698. avctx->active_thread_type = 0;
  699. return 0;
  700. }
  701. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  702. {
  703. AVCodec *p, *experimental = NULL;
  704. p = first_avcodec;
  705. while (p) {
  706. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  707. p->id == id) {
  708. if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
  709. experimental = p;
  710. } else
  711. return p;
  712. }
  713. p = p->next;
  714. }
  715. return experimental;
  716. }
  717. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  718. {
  719. return find_encdec(id, 1);
  720. }
  721. AVCodec *avcodec_find_encoder_by_name(const char *name)
  722. {
  723. AVCodec *p;
  724. if (!name)
  725. return NULL;
  726. p = first_avcodec;
  727. while (p) {
  728. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  729. return p;
  730. p = p->next;
  731. }
  732. return NULL;
  733. }
  734. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  735. {
  736. return find_encdec(id, 0);
  737. }
  738. AVCodec *avcodec_find_decoder_by_name(const char *name)
  739. {
  740. AVCodec *p;
  741. if (!name)
  742. return NULL;
  743. p = first_avcodec;
  744. while (p) {
  745. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  746. return p;
  747. p = p->next;
  748. }
  749. return NULL;
  750. }
  751. static int get_bit_rate(AVCodecContext *ctx)
  752. {
  753. int bit_rate;
  754. int bits_per_sample;
  755. switch (ctx->codec_type) {
  756. case AVMEDIA_TYPE_VIDEO:
  757. case AVMEDIA_TYPE_DATA:
  758. case AVMEDIA_TYPE_SUBTITLE:
  759. case AVMEDIA_TYPE_ATTACHMENT:
  760. bit_rate = ctx->bit_rate;
  761. break;
  762. case AVMEDIA_TYPE_AUDIO:
  763. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  764. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  765. break;
  766. default:
  767. bit_rate = 0;
  768. break;
  769. }
  770. return bit_rate;
  771. }
  772. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  773. {
  774. int i, len, ret = 0;
  775. #define TAG_PRINT(x) \
  776. (((x) >= '0' && (x) <= '9') || \
  777. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  778. ((x) == '.' || (x) == ' '))
  779. for (i = 0; i < 4; i++) {
  780. len = snprintf(buf, buf_size,
  781. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  782. buf += len;
  783. buf_size = buf_size > len ? buf_size - len : 0;
  784. ret += len;
  785. codec_tag >>= 8;
  786. }
  787. return ret;
  788. }
  789. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  790. {
  791. const char *codec_name;
  792. const char *profile = NULL;
  793. char buf1[32];
  794. int bitrate;
  795. int new_line = 0;
  796. AVRational display_aspect_ratio;
  797. const AVCodecDescriptor *desc = avcodec_descriptor_get(enc->codec_id);
  798. if (desc) {
  799. codec_name = desc->name;
  800. profile = avcodec_profile_name(enc->codec_id, enc->profile);
  801. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  802. /* fake mpeg2 transport stream codec (currently not
  803. * registered) */
  804. codec_name = "mpeg2ts";
  805. } else {
  806. /* output avi tags */
  807. char tag_buf[32];
  808. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  809. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  810. codec_name = buf1;
  811. }
  812. switch (enc->codec_type) {
  813. case AVMEDIA_TYPE_VIDEO:
  814. snprintf(buf, buf_size,
  815. "Video: %s%s",
  816. codec_name, enc->mb_decision ? " (hq)" : "");
  817. if (profile)
  818. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  819. " (%s)", profile);
  820. if (enc->codec_tag) {
  821. char tag_buf[32];
  822. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  823. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  824. " [%s / 0x%04X]", tag_buf, enc->codec_tag);
  825. }
  826. av_strlcat(buf, "\n ", buf_size);
  827. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  828. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  829. av_get_pix_fmt_name(enc->pix_fmt));
  830. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  831. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
  832. av_color_range_name(enc->color_range));
  833. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  834. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  835. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  836. new_line = 1;
  837. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s/%s/%s",
  838. av_color_space_name(enc->colorspace),
  839. av_color_primaries_name(enc->color_primaries),
  840. av_color_transfer_name(enc->color_trc));
  841. }
  842. if (av_log_get_level() >= AV_LOG_DEBUG &&
  843. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  844. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
  845. av_chroma_location_name(enc->chroma_sample_location));
  846. if (enc->width) {
  847. av_strlcat(buf, new_line ? "\n " : ", ", buf_size);
  848. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  849. "%dx%d",
  850. enc->width, enc->height);
  851. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  852. (enc->width != enc->coded_width ||
  853. enc->height != enc->coded_height))
  854. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  855. " (%dx%d)", enc->coded_width, enc->coded_height);
  856. if (enc->sample_aspect_ratio.num) {
  857. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  858. enc->width * enc->sample_aspect_ratio.num,
  859. enc->height * enc->sample_aspect_ratio.den,
  860. 1024 * 1024);
  861. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  862. " [PAR %d:%d DAR %d:%d]",
  863. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  864. display_aspect_ratio.num, display_aspect_ratio.den);
  865. }
  866. if (av_log_get_level() >= AV_LOG_DEBUG) {
  867. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  868. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  869. ", %d/%d",
  870. enc->time_base.num / g, enc->time_base.den / g);
  871. }
  872. }
  873. if (encode) {
  874. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  875. ", q=%d-%d", enc->qmin, enc->qmax);
  876. }
  877. break;
  878. case AVMEDIA_TYPE_AUDIO:
  879. snprintf(buf, buf_size,
  880. "Audio: %s",
  881. codec_name);
  882. if (profile)
  883. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  884. " (%s)", profile);
  885. if (enc->codec_tag) {
  886. char tag_buf[32];
  887. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  888. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  889. " [%s / 0x%04X]", tag_buf, enc->codec_tag);
  890. }
  891. av_strlcat(buf, "\n ", buf_size);
  892. if (enc->sample_rate) {
  893. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  894. "%d Hz, ", enc->sample_rate);
  895. }
  896. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  897. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  898. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  899. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  900. }
  901. break;
  902. case AVMEDIA_TYPE_DATA:
  903. snprintf(buf, buf_size, "Data: %s", codec_name);
  904. break;
  905. case AVMEDIA_TYPE_SUBTITLE:
  906. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  907. break;
  908. case AVMEDIA_TYPE_ATTACHMENT:
  909. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  910. break;
  911. default:
  912. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  913. return;
  914. }
  915. if (encode) {
  916. if (enc->flags & AV_CODEC_FLAG_PASS1)
  917. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  918. ", pass 1");
  919. if (enc->flags & AV_CODEC_FLAG_PASS2)
  920. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  921. ", pass 2");
  922. }
  923. bitrate = get_bit_rate(enc);
  924. if (bitrate != 0) {
  925. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  926. ", %d kb/s", bitrate / 1000);
  927. }
  928. }
  929. const char *av_get_profile_name(const AVCodec *codec, int profile)
  930. {
  931. const AVProfile *p;
  932. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  933. return NULL;
  934. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  935. if (p->profile == profile)
  936. return p->name;
  937. return NULL;
  938. }
  939. const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
  940. {
  941. const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
  942. const AVProfile *p;
  943. if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
  944. return NULL;
  945. for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  946. if (p->profile == profile)
  947. return p->name;
  948. return NULL;
  949. }
  950. unsigned avcodec_version(void)
  951. {
  952. return LIBAVCODEC_VERSION_INT;
  953. }
  954. const char *avcodec_configuration(void)
  955. {
  956. return LIBAV_CONFIGURATION;
  957. }
  958. const char *avcodec_license(void)
  959. {
  960. #define LICENSE_PREFIX "libavcodec license: "
  961. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  962. }
  963. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  964. {
  965. switch (codec_id) {
  966. case AV_CODEC_ID_ADPCM_CT:
  967. case AV_CODEC_ID_ADPCM_IMA_APC:
  968. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  969. case AV_CODEC_ID_ADPCM_IMA_WS:
  970. case AV_CODEC_ID_ADPCM_G722:
  971. case AV_CODEC_ID_ADPCM_YAMAHA:
  972. return 4;
  973. case AV_CODEC_ID_PCM_ALAW:
  974. case AV_CODEC_ID_PCM_MULAW:
  975. case AV_CODEC_ID_PCM_S8:
  976. case AV_CODEC_ID_PCM_U8:
  977. case AV_CODEC_ID_PCM_ZORK:
  978. return 8;
  979. case AV_CODEC_ID_PCM_S16BE:
  980. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  981. case AV_CODEC_ID_PCM_S16LE:
  982. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  983. case AV_CODEC_ID_PCM_U16BE:
  984. case AV_CODEC_ID_PCM_U16LE:
  985. return 16;
  986. case AV_CODEC_ID_PCM_S24DAUD:
  987. case AV_CODEC_ID_PCM_S24BE:
  988. case AV_CODEC_ID_PCM_S24LE:
  989. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  990. case AV_CODEC_ID_PCM_U24BE:
  991. case AV_CODEC_ID_PCM_U24LE:
  992. return 24;
  993. case AV_CODEC_ID_PCM_S32BE:
  994. case AV_CODEC_ID_PCM_S32LE:
  995. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  996. case AV_CODEC_ID_PCM_U32BE:
  997. case AV_CODEC_ID_PCM_U32LE:
  998. case AV_CODEC_ID_PCM_F32BE:
  999. case AV_CODEC_ID_PCM_F32LE:
  1000. return 32;
  1001. case AV_CODEC_ID_PCM_F64BE:
  1002. case AV_CODEC_ID_PCM_F64LE:
  1003. return 64;
  1004. default:
  1005. return 0;
  1006. }
  1007. }
  1008. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1009. {
  1010. switch (codec_id) {
  1011. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1012. return 2;
  1013. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1014. return 3;
  1015. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1016. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1017. case AV_CODEC_ID_ADPCM_IMA_QT:
  1018. case AV_CODEC_ID_ADPCM_SWF:
  1019. case AV_CODEC_ID_ADPCM_MS:
  1020. return 4;
  1021. default:
  1022. return av_get_exact_bits_per_sample(codec_id);
  1023. }
  1024. }
  1025. static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
  1026. uint32_t tag, int bits_per_coded_sample, int frame_bytes)
  1027. {
  1028. int bps = av_get_exact_bits_per_sample(id);
  1029. /* codecs with an exact constant bits per sample */
  1030. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1031. return (frame_bytes * 8) / (bps * ch);
  1032. bps = bits_per_coded_sample;
  1033. /* codecs with a fixed packet duration */
  1034. switch (id) {
  1035. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1036. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1037. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1038. case AV_CODEC_ID_AMR_NB:
  1039. case AV_CODEC_ID_GSM:
  1040. case AV_CODEC_ID_QCELP:
  1041. case AV_CODEC_ID_RA_144:
  1042. case AV_CODEC_ID_RA_288: return 160;
  1043. case AV_CODEC_ID_IMC: return 256;
  1044. case AV_CODEC_ID_AMR_WB:
  1045. case AV_CODEC_ID_GSM_MS: return 320;
  1046. case AV_CODEC_ID_MP1: return 384;
  1047. case AV_CODEC_ID_ATRAC1: return 512;
  1048. case AV_CODEC_ID_ATRAC3: return 1024;
  1049. case AV_CODEC_ID_MP2:
  1050. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1051. case AV_CODEC_ID_AC3: return 1536;
  1052. }
  1053. if (sr > 0) {
  1054. /* calc from sample rate */
  1055. if (id == AV_CODEC_ID_TTA)
  1056. return 256 * sr / 245;
  1057. if (ch > 0) {
  1058. /* calc from sample rate and channels */
  1059. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1060. return (480 << (sr / 22050)) / ch;
  1061. }
  1062. if (id == AV_CODEC_ID_MP3)
  1063. return sr <= 24000 ? 576 : 1152;
  1064. }
  1065. if (ba > 0) {
  1066. /* calc from block_align */
  1067. if (id == AV_CODEC_ID_SIPR) {
  1068. switch (ba) {
  1069. case 20: return 160;
  1070. case 19: return 144;
  1071. case 29: return 288;
  1072. case 37: return 480;
  1073. }
  1074. } else if (id == AV_CODEC_ID_ILBC) {
  1075. switch (ba) {
  1076. case 38: return 160;
  1077. case 50: return 240;
  1078. }
  1079. }
  1080. }
  1081. if (frame_bytes > 0) {
  1082. /* calc from frame_bytes only */
  1083. if (id == AV_CODEC_ID_TRUESPEECH)
  1084. return 240 * (frame_bytes / 32);
  1085. if (id == AV_CODEC_ID_NELLYMOSER)
  1086. return 256 * (frame_bytes / 64);
  1087. if (bps > 0) {
  1088. /* calc from frame_bytes and bits_per_coded_sample */
  1089. if (id == AV_CODEC_ID_ADPCM_G726)
  1090. return frame_bytes * 8 / bps;
  1091. }
  1092. if (ch > 0) {
  1093. /* calc from frame_bytes and channels */
  1094. switch (id) {
  1095. case AV_CODEC_ID_ADPCM_4XM:
  1096. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1097. return (frame_bytes - 4 * ch) * 2 / ch;
  1098. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1099. return (frame_bytes - 4) * 2 / ch;
  1100. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1101. return (frame_bytes - 8) * 2 / ch;
  1102. case AV_CODEC_ID_ADPCM_XA:
  1103. return (frame_bytes / 128) * 224 / ch;
  1104. case AV_CODEC_ID_INTERPLAY_DPCM:
  1105. return (frame_bytes - 6 - ch) / ch;
  1106. case AV_CODEC_ID_ROQ_DPCM:
  1107. return (frame_bytes - 8) / ch;
  1108. case AV_CODEC_ID_XAN_DPCM:
  1109. return (frame_bytes - 2 * ch) / ch;
  1110. case AV_CODEC_ID_MACE3:
  1111. return 3 * frame_bytes / ch;
  1112. case AV_CODEC_ID_MACE6:
  1113. return 6 * frame_bytes / ch;
  1114. case AV_CODEC_ID_PCM_LXF:
  1115. return 2 * (frame_bytes / (5 * ch));
  1116. }
  1117. if (tag) {
  1118. /* calc from frame_bytes, channels, and codec_tag */
  1119. if (id == AV_CODEC_ID_SOL_DPCM) {
  1120. if (tag == 3)
  1121. return frame_bytes / ch;
  1122. else
  1123. return frame_bytes * 2 / ch;
  1124. }
  1125. }
  1126. if (ba > 0) {
  1127. /* calc from frame_bytes, channels, and block_align */
  1128. int blocks = frame_bytes / ba;
  1129. switch (id) {
  1130. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1131. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1132. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1133. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1134. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1135. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1136. case AV_CODEC_ID_ADPCM_MS:
  1137. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1138. }
  1139. }
  1140. if (bps > 0) {
  1141. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1142. switch (id) {
  1143. case AV_CODEC_ID_PCM_DVD:
  1144. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1145. case AV_CODEC_ID_PCM_BLURAY:
  1146. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1147. case AV_CODEC_ID_S302M:
  1148. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1149. }
  1150. }
  1151. }
  1152. }
  1153. return 0;
  1154. }
  1155. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1156. {
  1157. return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
  1158. avctx->channels, avctx->block_align,
  1159. avctx->codec_tag, avctx->bits_per_coded_sample,
  1160. frame_bytes);
  1161. }
  1162. int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
  1163. {
  1164. return get_audio_frame_duration(par->codec_id, par->sample_rate,
  1165. par->channels, par->block_align,
  1166. par->codec_tag, par->bits_per_coded_sample,
  1167. frame_bytes);
  1168. }
  1169. #if !HAVE_THREADS
  1170. int ff_thread_init(AVCodecContext *s)
  1171. {
  1172. return -1;
  1173. }
  1174. #endif
  1175. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1176. {
  1177. unsigned int n = 0;
  1178. while (v >= 0xff) {
  1179. *s++ = 0xff;
  1180. v -= 0xff;
  1181. n++;
  1182. }
  1183. *s = v;
  1184. n++;
  1185. return n;
  1186. }
  1187. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1188. {
  1189. int i;
  1190. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1191. return i;
  1192. }
  1193. static AVHWAccel *first_hwaccel = NULL;
  1194. void av_register_hwaccel(AVHWAccel *hwaccel)
  1195. {
  1196. AVHWAccel **p = &first_hwaccel;
  1197. while (*p)
  1198. p = &(*p)->next;
  1199. *p = hwaccel;
  1200. hwaccel->next = NULL;
  1201. }
  1202. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  1203. {
  1204. return hwaccel ? hwaccel->next : first_hwaccel;
  1205. }
  1206. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1207. {
  1208. if (lockmgr_cb) {
  1209. // There is no good way to rollback a failure to destroy the
  1210. // mutex, so we ignore failures.
  1211. lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
  1212. lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
  1213. lockmgr_cb = NULL;
  1214. codec_mutex = NULL;
  1215. avformat_mutex = NULL;
  1216. }
  1217. if (cb) {
  1218. void *new_codec_mutex = NULL;
  1219. void *new_avformat_mutex = NULL;
  1220. int err;
  1221. if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
  1222. return err > 0 ? AVERROR_UNKNOWN : err;
  1223. }
  1224. if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
  1225. // Ignore failures to destroy the newly created mutex.
  1226. cb(&new_codec_mutex, AV_LOCK_DESTROY);
  1227. return err > 0 ? AVERROR_UNKNOWN : err;
  1228. }
  1229. lockmgr_cb = cb;
  1230. codec_mutex = new_codec_mutex;
  1231. avformat_mutex = new_avformat_mutex;
  1232. }
  1233. return 0;
  1234. }
  1235. int avpriv_lock_avformat(void)
  1236. {
  1237. if (lockmgr_cb) {
  1238. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1239. return -1;
  1240. }
  1241. return 0;
  1242. }
  1243. int avpriv_unlock_avformat(void)
  1244. {
  1245. if (lockmgr_cb) {
  1246. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1247. return -1;
  1248. }
  1249. return 0;
  1250. }
  1251. unsigned int avpriv_toupper4(unsigned int x)
  1252. {
  1253. return av_toupper(x & 0xFF) +
  1254. (av_toupper((x >> 8) & 0xFF) << 8) +
  1255. (av_toupper((x >> 16) & 0xFF) << 16) +
  1256. (av_toupper((x >> 24) & 0xFF) << 24);
  1257. }
  1258. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  1259. {
  1260. int ret;
  1261. dst->owner = src->owner;
  1262. ret = av_frame_ref(dst->f, src->f);
  1263. if (ret < 0)
  1264. return ret;
  1265. if (src->progress &&
  1266. !(dst->progress = av_buffer_ref(src->progress))) {
  1267. ff_thread_release_buffer(dst->owner, dst);
  1268. return AVERROR(ENOMEM);
  1269. }
  1270. return 0;
  1271. }
  1272. #if !HAVE_THREADS
  1273. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  1274. {
  1275. f->owner = avctx;
  1276. return ff_get_buffer(avctx, f->f, flags);
  1277. }
  1278. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  1279. {
  1280. if (f->f)
  1281. av_frame_unref(f->f);
  1282. }
  1283. void ff_thread_finish_setup(AVCodecContext *avctx)
  1284. {
  1285. }
  1286. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  1287. {
  1288. }
  1289. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  1290. {
  1291. }
  1292. #endif
  1293. int avcodec_is_open(AVCodecContext *s)
  1294. {
  1295. return !!s->internal;
  1296. }
  1297. const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
  1298. const uint8_t *end,
  1299. uint32_t * restrict state)
  1300. {
  1301. int i;
  1302. assert(p <= end);
  1303. if (p >= end)
  1304. return end;
  1305. for (i = 0; i < 3; i++) {
  1306. uint32_t tmp = *state << 8;
  1307. *state = tmp + *(p++);
  1308. if (tmp == 0x100 || p == end)
  1309. return p;
  1310. }
  1311. while (p < end) {
  1312. if (p[-1] > 1 ) p += 3;
  1313. else if (p[-2] ) p += 2;
  1314. else if (p[-3]|(p[-1]-1)) p++;
  1315. else {
  1316. p++;
  1317. break;
  1318. }
  1319. }
  1320. p = FFMIN(p, end) - 4;
  1321. *state = AV_RB32(p);
  1322. return p + 4;
  1323. }
  1324. AVCPBProperties *av_cpb_properties_alloc(size_t *size)
  1325. {
  1326. AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
  1327. if (!props)
  1328. return NULL;
  1329. if (size)
  1330. *size = sizeof(*props);
  1331. props->vbv_delay = UINT64_MAX;
  1332. return props;
  1333. }
  1334. AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
  1335. {
  1336. AVPacketSideData *tmp;
  1337. AVCPBProperties *props;
  1338. size_t size;
  1339. props = av_cpb_properties_alloc(&size);
  1340. if (!props)
  1341. return NULL;
  1342. tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
  1343. if (!tmp) {
  1344. av_freep(&props);
  1345. return NULL;
  1346. }
  1347. avctx->coded_side_data = tmp;
  1348. avctx->nb_coded_side_data++;
  1349. avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
  1350. avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
  1351. avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
  1352. return props;
  1353. }
  1354. static void codec_parameters_reset(AVCodecParameters *par)
  1355. {
  1356. av_freep(&par->extradata);
  1357. memset(par, 0, sizeof(*par));
  1358. par->codec_type = AVMEDIA_TYPE_UNKNOWN;
  1359. par->codec_id = AV_CODEC_ID_NONE;
  1360. par->format = -1;
  1361. par->field_order = AV_FIELD_UNKNOWN;
  1362. par->color_range = AVCOL_RANGE_UNSPECIFIED;
  1363. par->color_primaries = AVCOL_PRI_UNSPECIFIED;
  1364. par->color_trc = AVCOL_TRC_UNSPECIFIED;
  1365. par->color_space = AVCOL_SPC_UNSPECIFIED;
  1366. par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  1367. par->sample_aspect_ratio = (AVRational){ 0, 1 };
  1368. }
  1369. AVCodecParameters *avcodec_parameters_alloc(void)
  1370. {
  1371. AVCodecParameters *par = av_mallocz(sizeof(*par));
  1372. if (!par)
  1373. return NULL;
  1374. codec_parameters_reset(par);
  1375. return par;
  1376. }
  1377. void avcodec_parameters_free(AVCodecParameters **ppar)
  1378. {
  1379. AVCodecParameters *par = *ppar;
  1380. if (!par)
  1381. return;
  1382. codec_parameters_reset(par);
  1383. av_freep(ppar);
  1384. }
  1385. int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
  1386. {
  1387. codec_parameters_reset(dst);
  1388. memcpy(dst, src, sizeof(*dst));
  1389. dst->extradata = NULL;
  1390. dst->extradata_size = 0;
  1391. if (src->extradata) {
  1392. dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1393. if (!dst->extradata)
  1394. return AVERROR(ENOMEM);
  1395. memcpy(dst->extradata, src->extradata, src->extradata_size);
  1396. dst->extradata_size = src->extradata_size;
  1397. }
  1398. return 0;
  1399. }
  1400. int avcodec_parameters_from_context(AVCodecParameters *par,
  1401. const AVCodecContext *codec)
  1402. {
  1403. codec_parameters_reset(par);
  1404. par->codec_type = codec->codec_type;
  1405. par->codec_id = codec->codec_id;
  1406. par->codec_tag = codec->codec_tag;
  1407. par->bit_rate = codec->bit_rate;
  1408. par->bits_per_coded_sample = codec->bits_per_coded_sample;
  1409. par->profile = codec->profile;
  1410. par->level = codec->level;
  1411. switch (par->codec_type) {
  1412. case AVMEDIA_TYPE_VIDEO:
  1413. par->format = codec->pix_fmt;
  1414. par->width = codec->width;
  1415. par->height = codec->height;
  1416. par->field_order = codec->field_order;
  1417. par->color_range = codec->color_range;
  1418. par->color_primaries = codec->color_primaries;
  1419. par->color_trc = codec->color_trc;
  1420. par->color_space = codec->colorspace;
  1421. par->chroma_location = codec->chroma_sample_location;
  1422. par->sample_aspect_ratio = codec->sample_aspect_ratio;
  1423. break;
  1424. case AVMEDIA_TYPE_AUDIO:
  1425. par->format = codec->sample_fmt;
  1426. par->channel_layout = codec->channel_layout;
  1427. par->channels = codec->channels;
  1428. par->sample_rate = codec->sample_rate;
  1429. par->block_align = codec->block_align;
  1430. par->initial_padding = codec->initial_padding;
  1431. break;
  1432. }
  1433. if (codec->extradata) {
  1434. par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1435. if (!par->extradata)
  1436. return AVERROR(ENOMEM);
  1437. memcpy(par->extradata, codec->extradata, codec->extradata_size);
  1438. par->extradata_size = codec->extradata_size;
  1439. }
  1440. return 0;
  1441. }
  1442. int avcodec_parameters_to_context(AVCodecContext *codec,
  1443. const AVCodecParameters *par)
  1444. {
  1445. codec->codec_type = par->codec_type;
  1446. codec->codec_id = par->codec_id;
  1447. codec->codec_tag = par->codec_tag;
  1448. codec->bit_rate = par->bit_rate;
  1449. codec->bits_per_coded_sample = par->bits_per_coded_sample;
  1450. codec->profile = par->profile;
  1451. codec->level = par->level;
  1452. switch (par->codec_type) {
  1453. case AVMEDIA_TYPE_VIDEO:
  1454. codec->pix_fmt = par->format;
  1455. codec->width = par->width;
  1456. codec->height = par->height;
  1457. codec->field_order = par->field_order;
  1458. codec->color_range = par->color_range;
  1459. codec->color_primaries = par->color_primaries;
  1460. codec->color_trc = par->color_trc;
  1461. codec->colorspace = par->color_space;
  1462. codec->chroma_sample_location = par->chroma_location;
  1463. codec->sample_aspect_ratio = par->sample_aspect_ratio;
  1464. break;
  1465. case AVMEDIA_TYPE_AUDIO:
  1466. codec->sample_fmt = par->format;
  1467. codec->channel_layout = par->channel_layout;
  1468. codec->channels = par->channels;
  1469. codec->sample_rate = par->sample_rate;
  1470. codec->block_align = par->block_align;
  1471. codec->initial_padding = par->initial_padding;
  1472. break;
  1473. }
  1474. if (par->extradata) {
  1475. av_freep(&codec->extradata);
  1476. codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1477. if (!codec->extradata)
  1478. return AVERROR(ENOMEM);
  1479. memcpy(codec->extradata, par->extradata, par->extradata_size);
  1480. codec->extradata_size = par->extradata_size;
  1481. }
  1482. return 0;
  1483. }