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.

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