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.

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