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.

1747 lines
53KB

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