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.

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