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.

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