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.

2334 lines
70KB

  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/internal.h"
  34. #include "libavutil/mathematics.h"
  35. #include "libavutil/pixdesc.h"
  36. #include "libavutil/imgutils.h"
  37. #include "libavutil/samplefmt.h"
  38. #include "libavutil/dict.h"
  39. #include "avcodec.h"
  40. #include "dsputil.h"
  41. #include "libavutil/opt.h"
  42. #include "thread.h"
  43. #include "internal.h"
  44. #include "bytestream.h"
  45. #include "version.h"
  46. #include <stdlib.h>
  47. #include <stdarg.h>
  48. #include <limits.h>
  49. #include <float.h>
  50. static int volatile entangled_thread_counter = 0;
  51. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
  52. static void *codec_mutex;
  53. static void *avformat_mutex;
  54. #if FF_API_FAST_MALLOC && CONFIG_SHARED && HAVE_SYMVER
  55. FF_SYMVER(void*, av_fast_realloc, (void *ptr, unsigned int *size, size_t min_size), "LIBAVCODEC_55")
  56. {
  57. return av_fast_realloc(ptr, size, min_size);
  58. }
  59. FF_SYMVER(void, av_fast_malloc, (void *ptr, unsigned int *size, size_t min_size), "LIBAVCODEC_55")
  60. {
  61. av_fast_malloc(ptr, size, min_size);
  62. }
  63. #endif
  64. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  65. {
  66. void **p = ptr;
  67. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  68. av_freep(p);
  69. *size = 0;
  70. return;
  71. }
  72. av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  73. if (*size)
  74. memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  75. }
  76. /* encoder management */
  77. static AVCodec *first_avcodec = NULL;
  78. AVCodec *av_codec_next(const AVCodec *c)
  79. {
  80. if (c)
  81. return c->next;
  82. else
  83. return first_avcodec;
  84. }
  85. static av_cold void avcodec_init(void)
  86. {
  87. static int initialized = 0;
  88. if (initialized != 0)
  89. return;
  90. initialized = 1;
  91. if (CONFIG_DSPUTIL)
  92. ff_dsputil_static_init();
  93. }
  94. int av_codec_is_encoder(const AVCodec *codec)
  95. {
  96. return codec && (codec->encode_sub || codec->encode2);
  97. }
  98. int av_codec_is_decoder(const AVCodec *codec)
  99. {
  100. return codec && codec->decode;
  101. }
  102. av_cold void avcodec_register(AVCodec *codec)
  103. {
  104. AVCodec **p;
  105. avcodec_init();
  106. p = &first_avcodec;
  107. while (*p != NULL)
  108. p = &(*p)->next;
  109. *p = codec;
  110. codec->next = NULL;
  111. if (codec->init_static_data)
  112. codec->init_static_data(codec);
  113. }
  114. #if FF_API_EMU_EDGE
  115. unsigned avcodec_get_edge_width(void)
  116. {
  117. return EDGE_WIDTH;
  118. }
  119. #endif
  120. #if FF_API_SET_DIMENSIONS
  121. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  122. {
  123. ff_set_dimensions(s, width, height);
  124. }
  125. #endif
  126. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  127. {
  128. int ret = av_image_check_size(width, height, 0, s);
  129. if (ret < 0)
  130. width = height = 0;
  131. s->width = s->coded_width = width;
  132. s->height = s->coded_height = height;
  133. return ret;
  134. }
  135. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  136. enum AVMatrixEncoding matrix_encoding)
  137. {
  138. AVFrameSideData *side_data;
  139. enum AVMatrixEncoding *data;
  140. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
  141. if (!side_data)
  142. side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
  143. sizeof(enum AVMatrixEncoding));
  144. if (!side_data)
  145. return AVERROR(ENOMEM);
  146. data = (enum AVMatrixEncoding*)side_data->data;
  147. *data = matrix_encoding;
  148. return 0;
  149. }
  150. #if HAVE_NEON || ARCH_PPC || HAVE_MMX
  151. # define STRIDE_ALIGN 16
  152. #else
  153. # define STRIDE_ALIGN 8
  154. #endif
  155. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  156. int linesize_align[AV_NUM_DATA_POINTERS])
  157. {
  158. int i;
  159. int w_align = 1;
  160. int h_align = 1;
  161. switch (s->pix_fmt) {
  162. case AV_PIX_FMT_YUV420P:
  163. case AV_PIX_FMT_YUYV422:
  164. case AV_PIX_FMT_UYVY422:
  165. case AV_PIX_FMT_YUV422P:
  166. case AV_PIX_FMT_YUV440P:
  167. case AV_PIX_FMT_YUV444P:
  168. case AV_PIX_FMT_GBRP:
  169. case AV_PIX_FMT_GRAY8:
  170. case AV_PIX_FMT_GRAY16BE:
  171. case AV_PIX_FMT_GRAY16LE:
  172. case AV_PIX_FMT_YUVJ420P:
  173. case AV_PIX_FMT_YUVJ422P:
  174. case AV_PIX_FMT_YUVJ440P:
  175. case AV_PIX_FMT_YUVJ444P:
  176. case AV_PIX_FMT_YUVA420P:
  177. case AV_PIX_FMT_YUVA422P:
  178. case AV_PIX_FMT_YUVA444P:
  179. case AV_PIX_FMT_YUV420P9LE:
  180. case AV_PIX_FMT_YUV420P9BE:
  181. case AV_PIX_FMT_YUV420P10LE:
  182. case AV_PIX_FMT_YUV420P10BE:
  183. case AV_PIX_FMT_YUV422P9LE:
  184. case AV_PIX_FMT_YUV422P9BE:
  185. case AV_PIX_FMT_YUV422P10LE:
  186. case AV_PIX_FMT_YUV422P10BE:
  187. case AV_PIX_FMT_YUVA422P10LE:
  188. case AV_PIX_FMT_YUVA422P10BE:
  189. case AV_PIX_FMT_YUV444P9LE:
  190. case AV_PIX_FMT_YUV444P9BE:
  191. case AV_PIX_FMT_YUV444P10LE:
  192. case AV_PIX_FMT_YUV444P10BE:
  193. case AV_PIX_FMT_YUVA444P10LE:
  194. case AV_PIX_FMT_YUVA444P10BE:
  195. case AV_PIX_FMT_GBRP9LE:
  196. case AV_PIX_FMT_GBRP9BE:
  197. case AV_PIX_FMT_GBRP10LE:
  198. case AV_PIX_FMT_GBRP10BE:
  199. w_align = 16; //FIXME assume 16 pixel per macroblock
  200. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  201. break;
  202. case AV_PIX_FMT_YUV411P:
  203. case AV_PIX_FMT_UYYVYY411:
  204. w_align = 32;
  205. h_align = 8;
  206. break;
  207. case AV_PIX_FMT_YUV410P:
  208. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  209. w_align = 64;
  210. h_align = 64;
  211. }
  212. case AV_PIX_FMT_RGB555:
  213. if (s->codec_id == AV_CODEC_ID_RPZA) {
  214. w_align = 4;
  215. h_align = 4;
  216. }
  217. case AV_PIX_FMT_PAL8:
  218. case AV_PIX_FMT_BGR8:
  219. case AV_PIX_FMT_RGB8:
  220. if (s->codec_id == AV_CODEC_ID_SMC) {
  221. w_align = 4;
  222. h_align = 4;
  223. }
  224. break;
  225. case AV_PIX_FMT_BGR24:
  226. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  227. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  228. w_align = 4;
  229. h_align = 4;
  230. }
  231. break;
  232. default:
  233. w_align = 1;
  234. h_align = 1;
  235. break;
  236. }
  237. *width = FFALIGN(*width, w_align);
  238. *height = FFALIGN(*height, h_align);
  239. if (s->codec_id == AV_CODEC_ID_H264)
  240. // some of the optimized chroma MC reads one line too much
  241. *height += 2;
  242. for (i = 0; i < 4; i++)
  243. linesize_align[i] = STRIDE_ALIGN;
  244. }
  245. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  246. {
  247. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  248. int chroma_shift = desc->log2_chroma_w;
  249. int linesize_align[AV_NUM_DATA_POINTERS];
  250. int align;
  251. avcodec_align_dimensions2(s, width, height, linesize_align);
  252. align = FFMAX(linesize_align[0], linesize_align[3]);
  253. linesize_align[1] <<= chroma_shift;
  254. linesize_align[2] <<= chroma_shift;
  255. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  256. *width = FFALIGN(*width, align);
  257. }
  258. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  259. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  260. int buf_size, int align)
  261. {
  262. int ch, planar, needed_size, ret = 0;
  263. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  264. frame->nb_samples, sample_fmt,
  265. align);
  266. if (buf_size < needed_size)
  267. return AVERROR(EINVAL);
  268. planar = av_sample_fmt_is_planar(sample_fmt);
  269. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  270. if (!(frame->extended_data = av_mallocz(nb_channels *
  271. sizeof(*frame->extended_data))))
  272. return AVERROR(ENOMEM);
  273. } else {
  274. frame->extended_data = frame->data;
  275. }
  276. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  277. buf, nb_channels, frame->nb_samples,
  278. sample_fmt, align)) < 0) {
  279. if (frame->extended_data != frame->data)
  280. av_free(frame->extended_data);
  281. return ret;
  282. }
  283. if (frame->extended_data != frame->data) {
  284. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  285. frame->data[ch] = frame->extended_data[ch];
  286. }
  287. return ret;
  288. }
  289. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  290. {
  291. FramePool *pool = avctx->internal->pool;
  292. int i, ret;
  293. switch (avctx->codec_type) {
  294. case AVMEDIA_TYPE_VIDEO: {
  295. AVPicture picture;
  296. int size[4] = { 0 };
  297. int w = frame->width;
  298. int h = frame->height;
  299. int tmpsize, unaligned;
  300. if (pool->format == frame->format &&
  301. pool->width == frame->width && pool->height == frame->height)
  302. return 0;
  303. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  304. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  305. w += EDGE_WIDTH * 2;
  306. h += EDGE_WIDTH * 2;
  307. }
  308. do {
  309. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  310. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  311. av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
  312. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  313. w += w & ~(w - 1);
  314. unaligned = 0;
  315. for (i = 0; i < 4; i++)
  316. unaligned |= picture.linesize[i] % pool->stride_align[i];
  317. } while (unaligned);
  318. tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
  319. NULL, picture.linesize);
  320. if (tmpsize < 0)
  321. return -1;
  322. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  323. size[i] = picture.data[i + 1] - picture.data[i];
  324. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  325. for (i = 0; i < 4; i++) {
  326. av_buffer_pool_uninit(&pool->pools[i]);
  327. pool->linesize[i] = picture.linesize[i];
  328. if (size[i]) {
  329. pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
  330. if (!pool->pools[i]) {
  331. ret = AVERROR(ENOMEM);
  332. goto fail;
  333. }
  334. }
  335. }
  336. pool->format = frame->format;
  337. pool->width = frame->width;
  338. pool->height = frame->height;
  339. break;
  340. }
  341. case AVMEDIA_TYPE_AUDIO: {
  342. int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
  343. int planar = av_sample_fmt_is_planar(frame->format);
  344. int planes = planar ? ch : 1;
  345. if (pool->format == frame->format && pool->planes == planes &&
  346. pool->channels == ch && frame->nb_samples == pool->samples)
  347. return 0;
  348. av_buffer_pool_uninit(&pool->pools[0]);
  349. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  350. frame->nb_samples, frame->format, 0);
  351. if (ret < 0)
  352. goto fail;
  353. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  354. if (!pool->pools[0]) {
  355. ret = AVERROR(ENOMEM);
  356. goto fail;
  357. }
  358. pool->format = frame->format;
  359. pool->planes = planes;
  360. pool->channels = ch;
  361. pool->samples = frame->nb_samples;
  362. break;
  363. }
  364. default: av_assert0(0);
  365. }
  366. return 0;
  367. fail:
  368. for (i = 0; i < 4; i++)
  369. av_buffer_pool_uninit(&pool->pools[i]);
  370. pool->format = -1;
  371. pool->planes = pool->channels = pool->samples = 0;
  372. pool->width = pool->height = 0;
  373. return ret;
  374. }
  375. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  376. {
  377. FramePool *pool = avctx->internal->pool;
  378. int planes = pool->planes;
  379. int i;
  380. frame->linesize[0] = pool->linesize[0];
  381. if (planes > AV_NUM_DATA_POINTERS) {
  382. frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  383. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  384. frame->extended_buf = av_mallocz(frame->nb_extended_buf *
  385. sizeof(*frame->extended_buf));
  386. if (!frame->extended_data || !frame->extended_buf) {
  387. av_freep(&frame->extended_data);
  388. av_freep(&frame->extended_buf);
  389. return AVERROR(ENOMEM);
  390. }
  391. } else
  392. frame->extended_data = frame->data;
  393. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  394. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  395. if (!frame->buf[i])
  396. goto fail;
  397. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  398. }
  399. for (i = 0; i < frame->nb_extended_buf; i++) {
  400. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  401. if (!frame->extended_buf[i])
  402. goto fail;
  403. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  404. }
  405. if (avctx->debug & FF_DEBUG_BUFFERS)
  406. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  407. return 0;
  408. fail:
  409. av_frame_unref(frame);
  410. return AVERROR(ENOMEM);
  411. }
  412. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  413. {
  414. FramePool *pool = s->internal->pool;
  415. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
  416. int pixel_size = desc->comp[0].step_minus1 + 1;
  417. int h_chroma_shift, v_chroma_shift;
  418. int i;
  419. if (pic->data[0] != NULL) {
  420. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  421. return -1;
  422. }
  423. memset(pic->data, 0, sizeof(pic->data));
  424. pic->extended_data = pic->data;
  425. av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  426. for (i = 0; i < 4 && pool->pools[i]; i++) {
  427. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  428. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  429. pic->linesize[i] = pool->linesize[i];
  430. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  431. if (!pic->buf[i])
  432. goto fail;
  433. // no edge if EDGE EMU or not planar YUV
  434. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
  435. pic->data[i] = pic->buf[i]->data;
  436. else {
  437. pic->data[i] = pic->buf[i]->data +
  438. FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
  439. (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
  440. }
  441. }
  442. for (; i < AV_NUM_DATA_POINTERS; i++) {
  443. pic->data[i] = NULL;
  444. pic->linesize[i] = 0;
  445. }
  446. if (pic->data[1] && !pic->data[2])
  447. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  448. if (s->debug & FF_DEBUG_BUFFERS)
  449. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  450. return 0;
  451. fail:
  452. av_frame_unref(pic);
  453. return AVERROR(ENOMEM);
  454. }
  455. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  456. {
  457. int ret;
  458. if ((ret = update_frame_pool(avctx, frame)) < 0)
  459. return ret;
  460. #if FF_API_GET_BUFFER
  461. FF_DISABLE_DEPRECATION_WARNINGS
  462. frame->type = FF_BUFFER_TYPE_INTERNAL;
  463. FF_ENABLE_DEPRECATION_WARNINGS
  464. #endif
  465. switch (avctx->codec_type) {
  466. case AVMEDIA_TYPE_VIDEO:
  467. return video_get_buffer(avctx, frame);
  468. case AVMEDIA_TYPE_AUDIO:
  469. return audio_get_buffer(avctx, frame);
  470. default:
  471. return -1;
  472. }
  473. }
  474. #if FF_API_GET_BUFFER
  475. FF_DISABLE_DEPRECATION_WARNINGS
  476. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  477. {
  478. return avcodec_default_get_buffer2(avctx, frame, 0);
  479. }
  480. typedef struct CompatReleaseBufPriv {
  481. AVCodecContext avctx;
  482. AVFrame frame;
  483. } CompatReleaseBufPriv;
  484. static void compat_free_buffer(void *opaque, uint8_t *data)
  485. {
  486. CompatReleaseBufPriv *priv = opaque;
  487. if (priv->avctx.release_buffer)
  488. priv->avctx.release_buffer(&priv->avctx, &priv->frame);
  489. av_freep(&priv);
  490. }
  491. static void compat_release_buffer(void *opaque, uint8_t *data)
  492. {
  493. AVBufferRef *buf = opaque;
  494. av_buffer_unref(&buf);
  495. }
  496. FF_ENABLE_DEPRECATION_WARNINGS
  497. #endif
  498. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  499. {
  500. int override_dimensions = 1;
  501. int ret;
  502. switch (avctx->codec_type) {
  503. case AVMEDIA_TYPE_VIDEO:
  504. if (frame->width <= 0 || frame->height <= 0) {
  505. frame->width = FFMAX(avctx->width, avctx->coded_width);
  506. frame->height = FFMAX(avctx->height, avctx->coded_height);
  507. override_dimensions = 0;
  508. }
  509. if (frame->format < 0)
  510. frame->format = avctx->pix_fmt;
  511. if (!frame->sample_aspect_ratio.num)
  512. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  513. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  514. return ret;
  515. break;
  516. case AVMEDIA_TYPE_AUDIO:
  517. if (!frame->sample_rate)
  518. frame->sample_rate = avctx->sample_rate;
  519. if (frame->format < 0)
  520. frame->format = avctx->sample_fmt;
  521. if (!frame->channel_layout) {
  522. if (avctx->channel_layout) {
  523. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  524. avctx->channels) {
  525. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  526. "configuration.\n");
  527. return AVERROR(EINVAL);
  528. }
  529. frame->channel_layout = avctx->channel_layout;
  530. } else {
  531. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  532. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  533. avctx->channels);
  534. return AVERROR(ENOSYS);
  535. }
  536. frame->channel_layout = av_get_default_channel_layout(avctx->channels);
  537. if (!frame->channel_layout)
  538. frame->channel_layout = (1ULL << avctx->channels) - 1;
  539. }
  540. }
  541. break;
  542. default: return AVERROR(EINVAL);
  543. }
  544. frame->pkt_pts = avctx->internal->pkt ? avctx->internal->pkt->pts : AV_NOPTS_VALUE;
  545. frame->reordered_opaque = avctx->reordered_opaque;
  546. #if FF_API_GET_BUFFER
  547. FF_DISABLE_DEPRECATION_WARNINGS
  548. /*
  549. * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
  550. * We wrap each plane in its own AVBuffer. Each of those has a reference to
  551. * a dummy AVBuffer as its private data, unreffing it on free.
  552. * When all the planes are freed, the dummy buffer's free callback calls
  553. * release_buffer().
  554. */
  555. if (avctx->get_buffer) {
  556. CompatReleaseBufPriv *priv = NULL;
  557. AVBufferRef *dummy_buf = NULL;
  558. int planes, i, ret;
  559. if (flags & AV_GET_BUFFER_FLAG_REF)
  560. frame->reference = 1;
  561. ret = avctx->get_buffer(avctx, frame);
  562. if (ret < 0)
  563. return ret;
  564. /* return if the buffers are already set up
  565. * this would happen e.g. when a custom get_buffer() calls
  566. * avcodec_default_get_buffer
  567. */
  568. if (frame->buf[0])
  569. return 0;
  570. priv = av_mallocz(sizeof(*priv));
  571. if (!priv) {
  572. ret = AVERROR(ENOMEM);
  573. goto fail;
  574. }
  575. priv->avctx = *avctx;
  576. priv->frame = *frame;
  577. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
  578. if (!dummy_buf) {
  579. ret = AVERROR(ENOMEM);
  580. goto fail;
  581. }
  582. #define WRAP_PLANE(ref_out, data, data_size) \
  583. do { \
  584. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  585. if (!dummy_ref) { \
  586. ret = AVERROR(ENOMEM); \
  587. goto fail; \
  588. } \
  589. ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
  590. dummy_ref, 0); \
  591. if (!ref_out) { \
  592. av_frame_unref(frame); \
  593. ret = AVERROR(ENOMEM); \
  594. goto fail; \
  595. } \
  596. } while (0)
  597. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  598. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  599. planes = av_pix_fmt_count_planes(frame->format);
  600. /* workaround for AVHWAccel plane count of 0, buf[0] is used as
  601. check for allocated buffers: make libavcodec happy */
  602. if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  603. planes = 1;
  604. if (!desc || planes <= 0) {
  605. ret = AVERROR(EINVAL);
  606. goto fail;
  607. }
  608. for (i = 0; i < planes; i++) {
  609. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  610. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  611. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  612. }
  613. } else {
  614. int planar = av_sample_fmt_is_planar(frame->format);
  615. planes = planar ? avctx->channels : 1;
  616. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  617. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  618. frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
  619. frame->nb_extended_buf);
  620. if (!frame->extended_buf) {
  621. ret = AVERROR(ENOMEM);
  622. goto fail;
  623. }
  624. }
  625. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  626. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  627. for (i = 0; i < frame->nb_extended_buf; i++)
  628. WRAP_PLANE(frame->extended_buf[i],
  629. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  630. frame->linesize[0]);
  631. }
  632. av_buffer_unref(&dummy_buf);
  633. frame->width = avctx->width;
  634. frame->height = avctx->height;
  635. return 0;
  636. fail:
  637. avctx->release_buffer(avctx, frame);
  638. av_freep(&priv);
  639. av_buffer_unref(&dummy_buf);
  640. return ret;
  641. }
  642. FF_ENABLE_DEPRECATION_WARNINGS
  643. #endif
  644. ret = avctx->get_buffer2(avctx, frame, flags);
  645. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
  646. frame->width = avctx->width;
  647. frame->height = avctx->height;
  648. }
  649. return ret;
  650. }
  651. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  652. {
  653. AVFrame tmp;
  654. int ret;
  655. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  656. if (!frame->data[0])
  657. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  658. if (av_frame_is_writable(frame)) {
  659. frame->pkt_pts = avctx->internal->pkt ? avctx->internal->pkt->pts : AV_NOPTS_VALUE;
  660. frame->reordered_opaque = avctx->reordered_opaque;
  661. return 0;
  662. }
  663. av_frame_move_ref(&tmp, frame);
  664. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  665. if (ret < 0) {
  666. av_frame_unref(&tmp);
  667. return ret;
  668. }
  669. av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
  670. frame->format, frame->width, frame->height);
  671. av_frame_unref(&tmp);
  672. return 0;
  673. }
  674. #if FF_API_GET_BUFFER
  675. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  676. {
  677. av_frame_unref(pic);
  678. }
  679. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  680. {
  681. av_assert0(0);
  682. return AVERROR_BUG;
  683. }
  684. #endif
  685. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  686. {
  687. int i;
  688. for (i = 0; i < count; i++) {
  689. int r = func(c, (char *)arg + i * size);
  690. if (ret)
  691. ret[i] = r;
  692. }
  693. return 0;
  694. }
  695. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  696. {
  697. int i;
  698. for (i = 0; i < count; i++) {
  699. int r = func(c, arg, i, 0);
  700. if (ret)
  701. ret[i] = r;
  702. }
  703. return 0;
  704. }
  705. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  706. {
  707. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  708. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  709. }
  710. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  711. {
  712. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  713. ++fmt;
  714. return fmt[0];
  715. }
  716. #if FF_API_AVFRAME_LAVC
  717. void avcodec_get_frame_defaults(AVFrame *frame)
  718. {
  719. if (frame->extended_data != frame->data)
  720. av_freep(&frame->extended_data);
  721. memset(frame, 0, sizeof(AVFrame));
  722. frame->pts = AV_NOPTS_VALUE;
  723. frame->key_frame = 1;
  724. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  725. frame->format = -1; /* unknown */
  726. frame->extended_data = frame->data;
  727. }
  728. AVFrame *avcodec_alloc_frame(void)
  729. {
  730. AVFrame *frame = av_mallocz(sizeof(AVFrame));
  731. if (frame == NULL)
  732. return NULL;
  733. FF_DISABLE_DEPRECATION_WARNINGS
  734. avcodec_get_frame_defaults(frame);
  735. FF_ENABLE_DEPRECATION_WARNINGS
  736. return frame;
  737. }
  738. void avcodec_free_frame(AVFrame **frame)
  739. {
  740. av_frame_free(frame);
  741. }
  742. #endif
  743. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  744. {
  745. int ret = 0;
  746. AVDictionary *tmp = NULL;
  747. if (avcodec_is_open(avctx))
  748. return 0;
  749. if ((!codec && !avctx->codec)) {
  750. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  751. return AVERROR(EINVAL);
  752. }
  753. if ((codec && avctx->codec && codec != avctx->codec)) {
  754. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  755. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  756. return AVERROR(EINVAL);
  757. }
  758. if (!codec)
  759. codec = avctx->codec;
  760. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  761. return AVERROR(EINVAL);
  762. if (options)
  763. av_dict_copy(&tmp, *options, 0);
  764. /* If there is a user-supplied mutex locking routine, call it. */
  765. if (lockmgr_cb) {
  766. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  767. return -1;
  768. }
  769. entangled_thread_counter++;
  770. if (entangled_thread_counter != 1) {
  771. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  772. ret = -1;
  773. goto end;
  774. }
  775. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  776. if (!avctx->internal) {
  777. ret = AVERROR(ENOMEM);
  778. goto end;
  779. }
  780. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  781. if (!avctx->internal->pool) {
  782. ret = AVERROR(ENOMEM);
  783. goto free_and_end;
  784. }
  785. avctx->internal->to_free = av_frame_alloc();
  786. if (!avctx->internal->to_free) {
  787. ret = AVERROR(ENOMEM);
  788. goto free_and_end;
  789. }
  790. if (codec->priv_data_size > 0) {
  791. if (!avctx->priv_data) {
  792. avctx->priv_data = av_mallocz(codec->priv_data_size);
  793. if (!avctx->priv_data) {
  794. ret = AVERROR(ENOMEM);
  795. goto end;
  796. }
  797. if (codec->priv_class) {
  798. *(const AVClass **)avctx->priv_data = codec->priv_class;
  799. av_opt_set_defaults(avctx->priv_data);
  800. }
  801. }
  802. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  803. goto free_and_end;
  804. } else {
  805. avctx->priv_data = NULL;
  806. }
  807. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  808. goto free_and_end;
  809. if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
  810. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  811. else if (avctx->width && avctx->height)
  812. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  813. if (ret < 0)
  814. goto free_and_end;
  815. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  816. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  817. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  818. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  819. ff_set_dimensions(avctx, 0, 0);
  820. }
  821. /* if the decoder init function was already called previously,
  822. * free the already allocated subtitle_header before overwriting it */
  823. if (av_codec_is_decoder(codec))
  824. av_freep(&avctx->subtitle_header);
  825. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  826. ret = AVERROR(EINVAL);
  827. goto free_and_end;
  828. }
  829. avctx->codec = codec;
  830. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  831. avctx->codec_id == AV_CODEC_ID_NONE) {
  832. avctx->codec_type = codec->type;
  833. avctx->codec_id = codec->id;
  834. }
  835. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  836. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  837. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  838. ret = AVERROR(EINVAL);
  839. goto free_and_end;
  840. }
  841. avctx->frame_number = 0;
  842. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  843. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  844. ret = AVERROR_EXPERIMENTAL;
  845. goto free_and_end;
  846. }
  847. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  848. (!avctx->time_base.num || !avctx->time_base.den)) {
  849. avctx->time_base.num = 1;
  850. avctx->time_base.den = avctx->sample_rate;
  851. }
  852. if (HAVE_THREADS) {
  853. ret = ff_thread_init(avctx);
  854. if (ret < 0) {
  855. goto free_and_end;
  856. }
  857. }
  858. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  859. avctx->thread_count = 1;
  860. if (av_codec_is_encoder(avctx->codec)) {
  861. int i;
  862. if (avctx->codec->sample_fmts) {
  863. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  864. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  865. break;
  866. if (avctx->channels == 1 &&
  867. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  868. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  869. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  870. break;
  871. }
  872. }
  873. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  874. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  875. ret = AVERROR(EINVAL);
  876. goto free_and_end;
  877. }
  878. }
  879. if (avctx->codec->pix_fmts) {
  880. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  881. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  882. break;
  883. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
  884. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  885. ret = AVERROR(EINVAL);
  886. goto free_and_end;
  887. }
  888. }
  889. if (avctx->codec->supported_samplerates) {
  890. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  891. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  892. break;
  893. if (avctx->codec->supported_samplerates[i] == 0) {
  894. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  895. ret = AVERROR(EINVAL);
  896. goto free_and_end;
  897. }
  898. }
  899. if (avctx->codec->channel_layouts) {
  900. if (!avctx->channel_layout) {
  901. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  902. } else {
  903. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  904. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  905. break;
  906. if (avctx->codec->channel_layouts[i] == 0) {
  907. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  908. ret = AVERROR(EINVAL);
  909. goto free_and_end;
  910. }
  911. }
  912. }
  913. if (avctx->channel_layout && avctx->channels) {
  914. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  915. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  916. ret = AVERROR(EINVAL);
  917. goto free_and_end;
  918. }
  919. } else if (avctx->channel_layout) {
  920. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  921. }
  922. if (!avctx->rc_initial_buffer_occupancy)
  923. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  924. }
  925. if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  926. ret = avctx->codec->init(avctx);
  927. if (ret < 0) {
  928. goto free_and_end;
  929. }
  930. }
  931. if (av_codec_is_decoder(avctx->codec)) {
  932. /* validate channel layout from the decoder */
  933. if (avctx->channel_layout) {
  934. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  935. if (!avctx->channels)
  936. avctx->channels = channels;
  937. else if (channels != avctx->channels) {
  938. av_log(avctx, AV_LOG_WARNING,
  939. "channel layout does not match number of channels\n");
  940. avctx->channel_layout = 0;
  941. }
  942. }
  943. if (avctx->channels && avctx->channels < 0 ||
  944. avctx->channels > FF_SANE_NB_CHANNELS) {
  945. ret = AVERROR(EINVAL);
  946. goto free_and_end;
  947. }
  948. }
  949. end:
  950. entangled_thread_counter--;
  951. /* Release any user-supplied mutex. */
  952. if (lockmgr_cb) {
  953. (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  954. }
  955. if (options) {
  956. av_dict_free(options);
  957. *options = tmp;
  958. }
  959. return ret;
  960. free_and_end:
  961. av_dict_free(&tmp);
  962. av_freep(&avctx->priv_data);
  963. if (avctx->internal) {
  964. av_frame_free(&avctx->internal->to_free);
  965. av_freep(&avctx->internal->pool);
  966. }
  967. av_freep(&avctx->internal);
  968. avctx->codec = NULL;
  969. goto end;
  970. }
  971. int ff_alloc_packet(AVPacket *avpkt, int size)
  972. {
  973. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  974. return AVERROR(EINVAL);
  975. if (avpkt->data) {
  976. AVBufferRef *buf = avpkt->buf;
  977. #if FF_API_DESTRUCT_PACKET
  978. FF_DISABLE_DEPRECATION_WARNINGS
  979. void *destruct = avpkt->destruct;
  980. FF_ENABLE_DEPRECATION_WARNINGS
  981. #endif
  982. if (avpkt->size < size)
  983. return AVERROR(EINVAL);
  984. av_init_packet(avpkt);
  985. #if FF_API_DESTRUCT_PACKET
  986. FF_DISABLE_DEPRECATION_WARNINGS
  987. avpkt->destruct = destruct;
  988. FF_ENABLE_DEPRECATION_WARNINGS
  989. #endif
  990. avpkt->buf = buf;
  991. avpkt->size = size;
  992. return 0;
  993. } else {
  994. return av_new_packet(avpkt, size);
  995. }
  996. }
  997. /**
  998. * Pad last frame with silence.
  999. */
  1000. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1001. {
  1002. AVFrame *frame = NULL;
  1003. int ret;
  1004. if (!(frame = av_frame_alloc()))
  1005. return AVERROR(ENOMEM);
  1006. frame->format = src->format;
  1007. frame->channel_layout = src->channel_layout;
  1008. frame->nb_samples = s->frame_size;
  1009. ret = av_frame_get_buffer(frame, 32);
  1010. if (ret < 0)
  1011. goto fail;
  1012. ret = av_frame_copy_props(frame, src);
  1013. if (ret < 0)
  1014. goto fail;
  1015. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1016. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1017. goto fail;
  1018. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1019. frame->nb_samples - src->nb_samples,
  1020. s->channels, s->sample_fmt)) < 0)
  1021. goto fail;
  1022. *dst = frame;
  1023. return 0;
  1024. fail:
  1025. av_frame_free(&frame);
  1026. return ret;
  1027. }
  1028. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1029. AVPacket *avpkt,
  1030. const AVFrame *frame,
  1031. int *got_packet_ptr)
  1032. {
  1033. AVFrame tmp;
  1034. AVFrame *padded_frame = NULL;
  1035. int ret;
  1036. int user_packet = !!avpkt->data;
  1037. *got_packet_ptr = 0;
  1038. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1039. av_free_packet(avpkt);
  1040. av_init_packet(avpkt);
  1041. return 0;
  1042. }
  1043. /* ensure that extended_data is properly set */
  1044. if (frame && !frame->extended_data) {
  1045. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1046. avctx->channels > AV_NUM_DATA_POINTERS) {
  1047. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1048. "with more than %d channels, but extended_data is not set.\n",
  1049. AV_NUM_DATA_POINTERS);
  1050. return AVERROR(EINVAL);
  1051. }
  1052. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1053. tmp = *frame;
  1054. tmp.extended_data = tmp.data;
  1055. frame = &tmp;
  1056. }
  1057. /* check for valid frame size */
  1058. if (frame) {
  1059. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1060. if (frame->nb_samples > avctx->frame_size)
  1061. return AVERROR(EINVAL);
  1062. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1063. if (frame->nb_samples < avctx->frame_size &&
  1064. !avctx->internal->last_audio_frame) {
  1065. ret = pad_last_frame(avctx, &padded_frame, frame);
  1066. if (ret < 0)
  1067. return ret;
  1068. frame = padded_frame;
  1069. avctx->internal->last_audio_frame = 1;
  1070. }
  1071. if (frame->nb_samples != avctx->frame_size) {
  1072. ret = AVERROR(EINVAL);
  1073. goto end;
  1074. }
  1075. }
  1076. }
  1077. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1078. if (!ret) {
  1079. if (*got_packet_ptr) {
  1080. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1081. if (avpkt->pts == AV_NOPTS_VALUE)
  1082. avpkt->pts = frame->pts;
  1083. if (!avpkt->duration)
  1084. avpkt->duration = ff_samples_to_time_base(avctx,
  1085. frame->nb_samples);
  1086. }
  1087. avpkt->dts = avpkt->pts;
  1088. } else {
  1089. avpkt->size = 0;
  1090. }
  1091. if (!user_packet && avpkt->size) {
  1092. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  1093. if (ret >= 0)
  1094. avpkt->data = avpkt->buf->data;
  1095. }
  1096. avctx->frame_number++;
  1097. }
  1098. if (ret < 0 || !*got_packet_ptr) {
  1099. av_free_packet(avpkt);
  1100. av_init_packet(avpkt);
  1101. goto end;
  1102. }
  1103. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1104. * this needs to be moved to the encoders, but for now we can do it
  1105. * here to simplify things */
  1106. avpkt->flags |= AV_PKT_FLAG_KEY;
  1107. end:
  1108. av_frame_free(&padded_frame);
  1109. return ret;
  1110. }
  1111. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1112. AVPacket *avpkt,
  1113. const AVFrame *frame,
  1114. int *got_packet_ptr)
  1115. {
  1116. int ret;
  1117. int user_packet = !!avpkt->data;
  1118. *got_packet_ptr = 0;
  1119. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1120. av_free_packet(avpkt);
  1121. av_init_packet(avpkt);
  1122. avpkt->size = 0;
  1123. return 0;
  1124. }
  1125. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1126. return AVERROR(EINVAL);
  1127. av_assert0(avctx->codec->encode2);
  1128. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1129. if (!ret) {
  1130. if (!*got_packet_ptr)
  1131. avpkt->size = 0;
  1132. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1133. avpkt->pts = avpkt->dts = frame->pts;
  1134. if (!user_packet && avpkt->size) {
  1135. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  1136. if (ret >= 0)
  1137. avpkt->data = avpkt->buf->data;
  1138. }
  1139. avctx->frame_number++;
  1140. }
  1141. if (ret < 0 || !*got_packet_ptr)
  1142. av_free_packet(avpkt);
  1143. emms_c();
  1144. return ret;
  1145. }
  1146. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1147. const AVSubtitle *sub)
  1148. {
  1149. int ret;
  1150. if (sub->start_display_time) {
  1151. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1152. return -1;
  1153. }
  1154. if (sub->num_rects == 0 || !sub->rects)
  1155. return -1;
  1156. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1157. avctx->frame_number++;
  1158. return ret;
  1159. }
  1160. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1161. {
  1162. int size = 0, ret;
  1163. const uint8_t *data;
  1164. uint32_t flags;
  1165. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1166. if (!data)
  1167. return 0;
  1168. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
  1169. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  1170. "changes, but PARAM_CHANGE side data was sent to it.\n");
  1171. return AVERROR(EINVAL);
  1172. }
  1173. if (size < 4)
  1174. goto fail;
  1175. flags = bytestream_get_le32(&data);
  1176. size -= 4;
  1177. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1178. if (size < 4)
  1179. goto fail;
  1180. avctx->channels = bytestream_get_le32(&data);
  1181. size -= 4;
  1182. }
  1183. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1184. if (size < 8)
  1185. goto fail;
  1186. avctx->channel_layout = bytestream_get_le64(&data);
  1187. size -= 8;
  1188. }
  1189. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1190. if (size < 4)
  1191. goto fail;
  1192. avctx->sample_rate = bytestream_get_le32(&data);
  1193. size -= 4;
  1194. }
  1195. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1196. if (size < 8)
  1197. goto fail;
  1198. avctx->width = bytestream_get_le32(&data);
  1199. avctx->height = bytestream_get_le32(&data);
  1200. size -= 8;
  1201. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1202. if (ret < 0)
  1203. return ret;
  1204. }
  1205. return 0;
  1206. fail:
  1207. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  1208. return AVERROR_INVALIDDATA;
  1209. }
  1210. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  1211. {
  1212. int ret;
  1213. /* move the original frame to our backup */
  1214. av_frame_unref(avci->to_free);
  1215. av_frame_move_ref(avci->to_free, frame);
  1216. /* now copy everything except the AVBufferRefs back
  1217. * note that we make a COPY of the side data, so calling av_frame_free() on
  1218. * the caller's frame will work properly */
  1219. ret = av_frame_copy_props(frame, avci->to_free);
  1220. if (ret < 0)
  1221. return ret;
  1222. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  1223. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  1224. if (avci->to_free->extended_data != avci->to_free->data) {
  1225. int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
  1226. int size = planes * sizeof(*frame->extended_data);
  1227. if (!size) {
  1228. av_frame_unref(frame);
  1229. return AVERROR_BUG;
  1230. }
  1231. frame->extended_data = av_malloc(size);
  1232. if (!frame->extended_data) {
  1233. av_frame_unref(frame);
  1234. return AVERROR(ENOMEM);
  1235. }
  1236. memcpy(frame->extended_data, avci->to_free->extended_data,
  1237. size);
  1238. } else
  1239. frame->extended_data = frame->data;
  1240. frame->format = avci->to_free->format;
  1241. frame->width = avci->to_free->width;
  1242. frame->height = avci->to_free->height;
  1243. frame->channel_layout = avci->to_free->channel_layout;
  1244. frame->nb_samples = avci->to_free->nb_samples;
  1245. return 0;
  1246. }
  1247. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1248. int *got_picture_ptr,
  1249. AVPacket *avpkt)
  1250. {
  1251. AVCodecInternal *avci = avctx->internal;
  1252. int ret;
  1253. *got_picture_ptr = 0;
  1254. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1255. return -1;
  1256. avctx->internal->pkt = avpkt;
  1257. ret = apply_param_change(avctx, avpkt);
  1258. if (ret < 0) {
  1259. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1260. if (avctx->err_recognition & AV_EF_EXPLODE)
  1261. return ret;
  1262. }
  1263. av_frame_unref(picture);
  1264. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1265. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1266. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1267. avpkt);
  1268. else {
  1269. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1270. avpkt);
  1271. picture->pkt_dts = avpkt->dts;
  1272. /* get_buffer is supposed to set frame parameters */
  1273. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1274. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1275. picture->width = avctx->width;
  1276. picture->height = avctx->height;
  1277. picture->format = avctx->pix_fmt;
  1278. }
  1279. }
  1280. emms_c(); //needed to avoid an emms_c() call before every return;
  1281. if (*got_picture_ptr) {
  1282. if (!avctx->refcounted_frames) {
  1283. int err = unrefcount_frame(avci, picture);
  1284. if (err < 0)
  1285. return err;
  1286. }
  1287. avctx->frame_number++;
  1288. } else
  1289. av_frame_unref(picture);
  1290. } else
  1291. ret = 0;
  1292. return ret;
  1293. }
  1294. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1295. AVFrame *frame,
  1296. int *got_frame_ptr,
  1297. AVPacket *avpkt)
  1298. {
  1299. AVCodecInternal *avci = avctx->internal;
  1300. int ret = 0;
  1301. *got_frame_ptr = 0;
  1302. avctx->internal->pkt = avpkt;
  1303. if (!avpkt->data && avpkt->size) {
  1304. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1305. return AVERROR(EINVAL);
  1306. }
  1307. ret = apply_param_change(avctx, avpkt);
  1308. if (ret < 0) {
  1309. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1310. if (avctx->err_recognition & AV_EF_EXPLODE)
  1311. return ret;
  1312. }
  1313. av_frame_unref(frame);
  1314. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1315. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1316. if (ret >= 0 && *got_frame_ptr) {
  1317. avctx->frame_number++;
  1318. frame->pkt_dts = avpkt->dts;
  1319. if (frame->format == AV_SAMPLE_FMT_NONE)
  1320. frame->format = avctx->sample_fmt;
  1321. if (!avctx->refcounted_frames) {
  1322. int err = unrefcount_frame(avci, frame);
  1323. if (err < 0)
  1324. return err;
  1325. }
  1326. } else
  1327. av_frame_unref(frame);
  1328. }
  1329. return ret;
  1330. }
  1331. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1332. int *got_sub_ptr,
  1333. AVPacket *avpkt)
  1334. {
  1335. int ret;
  1336. avctx->internal->pkt = avpkt;
  1337. *got_sub_ptr = 0;
  1338. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1339. if (*got_sub_ptr)
  1340. avctx->frame_number++;
  1341. return ret;
  1342. }
  1343. void avsubtitle_free(AVSubtitle *sub)
  1344. {
  1345. int i;
  1346. for (i = 0; i < sub->num_rects; i++) {
  1347. av_freep(&sub->rects[i]->pict.data[0]);
  1348. av_freep(&sub->rects[i]->pict.data[1]);
  1349. av_freep(&sub->rects[i]->pict.data[2]);
  1350. av_freep(&sub->rects[i]->pict.data[3]);
  1351. av_freep(&sub->rects[i]->text);
  1352. av_freep(&sub->rects[i]->ass);
  1353. av_freep(&sub->rects[i]);
  1354. }
  1355. av_freep(&sub->rects);
  1356. memset(sub, 0, sizeof(AVSubtitle));
  1357. }
  1358. av_cold int avcodec_close(AVCodecContext *avctx)
  1359. {
  1360. /* If there is a user-supplied mutex locking routine, call it. */
  1361. if (lockmgr_cb) {
  1362. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1363. return -1;
  1364. }
  1365. entangled_thread_counter++;
  1366. if (entangled_thread_counter != 1) {
  1367. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1368. entangled_thread_counter--;
  1369. return -1;
  1370. }
  1371. if (avcodec_is_open(avctx)) {
  1372. FramePool *pool = avctx->internal->pool;
  1373. int i;
  1374. if (HAVE_THREADS && avctx->internal->thread_ctx)
  1375. ff_thread_free(avctx);
  1376. if (avctx->codec && avctx->codec->close)
  1377. avctx->codec->close(avctx);
  1378. avctx->coded_frame = NULL;
  1379. av_frame_free(&avctx->internal->to_free);
  1380. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  1381. av_buffer_pool_uninit(&pool->pools[i]);
  1382. av_freep(&avctx->internal->pool);
  1383. av_freep(&avctx->internal);
  1384. }
  1385. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1386. av_opt_free(avctx->priv_data);
  1387. av_opt_free(avctx);
  1388. av_freep(&avctx->priv_data);
  1389. if (av_codec_is_encoder(avctx->codec))
  1390. av_freep(&avctx->extradata);
  1391. avctx->codec = NULL;
  1392. avctx->active_thread_type = 0;
  1393. entangled_thread_counter--;
  1394. /* Release any user-supplied mutex. */
  1395. if (lockmgr_cb) {
  1396. (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1397. }
  1398. return 0;
  1399. }
  1400. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1401. {
  1402. AVCodec *p, *experimental = NULL;
  1403. p = first_avcodec;
  1404. while (p) {
  1405. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1406. p->id == id) {
  1407. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1408. experimental = p;
  1409. } else
  1410. return p;
  1411. }
  1412. p = p->next;
  1413. }
  1414. return experimental;
  1415. }
  1416. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1417. {
  1418. return find_encdec(id, 1);
  1419. }
  1420. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1421. {
  1422. AVCodec *p;
  1423. if (!name)
  1424. return NULL;
  1425. p = first_avcodec;
  1426. while (p) {
  1427. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1428. return p;
  1429. p = p->next;
  1430. }
  1431. return NULL;
  1432. }
  1433. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1434. {
  1435. return find_encdec(id, 0);
  1436. }
  1437. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1438. {
  1439. AVCodec *p;
  1440. if (!name)
  1441. return NULL;
  1442. p = first_avcodec;
  1443. while (p) {
  1444. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1445. return p;
  1446. p = p->next;
  1447. }
  1448. return NULL;
  1449. }
  1450. static int get_bit_rate(AVCodecContext *ctx)
  1451. {
  1452. int bit_rate;
  1453. int bits_per_sample;
  1454. switch (ctx->codec_type) {
  1455. case AVMEDIA_TYPE_VIDEO:
  1456. case AVMEDIA_TYPE_DATA:
  1457. case AVMEDIA_TYPE_SUBTITLE:
  1458. case AVMEDIA_TYPE_ATTACHMENT:
  1459. bit_rate = ctx->bit_rate;
  1460. break;
  1461. case AVMEDIA_TYPE_AUDIO:
  1462. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1463. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1464. break;
  1465. default:
  1466. bit_rate = 0;
  1467. break;
  1468. }
  1469. return bit_rate;
  1470. }
  1471. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1472. {
  1473. int i, len, ret = 0;
  1474. #define TAG_PRINT(x) \
  1475. (((x) >= '0' && (x) <= '9') || \
  1476. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1477. ((x) == '.' || (x) == ' '))
  1478. for (i = 0; i < 4; i++) {
  1479. len = snprintf(buf, buf_size,
  1480. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1481. buf += len;
  1482. buf_size = buf_size > len ? buf_size - len : 0;
  1483. ret += len;
  1484. codec_tag >>= 8;
  1485. }
  1486. return ret;
  1487. }
  1488. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1489. {
  1490. const char *codec_name;
  1491. const char *profile = NULL;
  1492. const AVCodec *p;
  1493. char buf1[32];
  1494. int bitrate;
  1495. AVRational display_aspect_ratio;
  1496. if (enc->codec)
  1497. p = enc->codec;
  1498. else if (encode)
  1499. p = avcodec_find_encoder(enc->codec_id);
  1500. else
  1501. p = avcodec_find_decoder(enc->codec_id);
  1502. if (p) {
  1503. codec_name = p->name;
  1504. profile = av_get_profile_name(p, enc->profile);
  1505. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1506. /* fake mpeg2 transport stream codec (currently not
  1507. * registered) */
  1508. codec_name = "mpeg2ts";
  1509. } else if (enc->codec_name[0] != '\0') {
  1510. codec_name = enc->codec_name;
  1511. } else {
  1512. /* output avi tags */
  1513. char tag_buf[32];
  1514. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1515. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1516. codec_name = buf1;
  1517. }
  1518. switch (enc->codec_type) {
  1519. case AVMEDIA_TYPE_VIDEO:
  1520. snprintf(buf, buf_size,
  1521. "Video: %s%s",
  1522. codec_name, enc->mb_decision ? " (hq)" : "");
  1523. if (profile)
  1524. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1525. " (%s)", profile);
  1526. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1527. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1528. ", %s",
  1529. av_get_pix_fmt_name(enc->pix_fmt));
  1530. }
  1531. if (enc->width) {
  1532. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1533. ", %dx%d",
  1534. enc->width, enc->height);
  1535. if (enc->sample_aspect_ratio.num) {
  1536. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1537. enc->width * enc->sample_aspect_ratio.num,
  1538. enc->height * enc->sample_aspect_ratio.den,
  1539. 1024 * 1024);
  1540. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1541. " [PAR %d:%d DAR %d:%d]",
  1542. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1543. display_aspect_ratio.num, display_aspect_ratio.den);
  1544. }
  1545. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1546. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1547. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1548. ", %d/%d",
  1549. enc->time_base.num / g, enc->time_base.den / g);
  1550. }
  1551. }
  1552. if (encode) {
  1553. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1554. ", q=%d-%d", enc->qmin, enc->qmax);
  1555. }
  1556. break;
  1557. case AVMEDIA_TYPE_AUDIO:
  1558. snprintf(buf, buf_size,
  1559. "Audio: %s",
  1560. codec_name);
  1561. if (profile)
  1562. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1563. " (%s)", profile);
  1564. if (enc->sample_rate) {
  1565. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1566. ", %d Hz", enc->sample_rate);
  1567. }
  1568. av_strlcat(buf, ", ", buf_size);
  1569. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1570. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1571. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1572. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1573. }
  1574. break;
  1575. case AVMEDIA_TYPE_DATA:
  1576. snprintf(buf, buf_size, "Data: %s", codec_name);
  1577. break;
  1578. case AVMEDIA_TYPE_SUBTITLE:
  1579. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1580. break;
  1581. case AVMEDIA_TYPE_ATTACHMENT:
  1582. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1583. break;
  1584. default:
  1585. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1586. return;
  1587. }
  1588. if (encode) {
  1589. if (enc->flags & CODEC_FLAG_PASS1)
  1590. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1591. ", pass 1");
  1592. if (enc->flags & CODEC_FLAG_PASS2)
  1593. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1594. ", pass 2");
  1595. }
  1596. bitrate = get_bit_rate(enc);
  1597. if (bitrate != 0) {
  1598. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1599. ", %d kb/s", bitrate / 1000);
  1600. }
  1601. }
  1602. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1603. {
  1604. const AVProfile *p;
  1605. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1606. return NULL;
  1607. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1608. if (p->profile == profile)
  1609. return p->name;
  1610. return NULL;
  1611. }
  1612. unsigned avcodec_version(void)
  1613. {
  1614. return LIBAVCODEC_VERSION_INT;
  1615. }
  1616. const char *avcodec_configuration(void)
  1617. {
  1618. return LIBAV_CONFIGURATION;
  1619. }
  1620. const char *avcodec_license(void)
  1621. {
  1622. #define LICENSE_PREFIX "libavcodec license: "
  1623. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1624. }
  1625. void avcodec_flush_buffers(AVCodecContext *avctx)
  1626. {
  1627. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1628. ff_thread_flush(avctx);
  1629. else if (avctx->codec->flush)
  1630. avctx->codec->flush(avctx);
  1631. if (!avctx->refcounted_frames)
  1632. av_frame_unref(avctx->internal->to_free);
  1633. }
  1634. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1635. {
  1636. switch (codec_id) {
  1637. case AV_CODEC_ID_ADPCM_CT:
  1638. case AV_CODEC_ID_ADPCM_IMA_APC:
  1639. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1640. case AV_CODEC_ID_ADPCM_IMA_WS:
  1641. case AV_CODEC_ID_ADPCM_G722:
  1642. case AV_CODEC_ID_ADPCM_YAMAHA:
  1643. return 4;
  1644. case AV_CODEC_ID_PCM_ALAW:
  1645. case AV_CODEC_ID_PCM_MULAW:
  1646. case AV_CODEC_ID_PCM_S8:
  1647. case AV_CODEC_ID_PCM_U8:
  1648. case AV_CODEC_ID_PCM_ZORK:
  1649. return 8;
  1650. case AV_CODEC_ID_PCM_S16BE:
  1651. case AV_CODEC_ID_PCM_S16LE:
  1652. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1653. case AV_CODEC_ID_PCM_U16BE:
  1654. case AV_CODEC_ID_PCM_U16LE:
  1655. return 16;
  1656. case AV_CODEC_ID_PCM_S24DAUD:
  1657. case AV_CODEC_ID_PCM_S24BE:
  1658. case AV_CODEC_ID_PCM_S24LE:
  1659. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  1660. case AV_CODEC_ID_PCM_U24BE:
  1661. case AV_CODEC_ID_PCM_U24LE:
  1662. return 24;
  1663. case AV_CODEC_ID_PCM_S32BE:
  1664. case AV_CODEC_ID_PCM_S32LE:
  1665. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  1666. case AV_CODEC_ID_PCM_U32BE:
  1667. case AV_CODEC_ID_PCM_U32LE:
  1668. case AV_CODEC_ID_PCM_F32BE:
  1669. case AV_CODEC_ID_PCM_F32LE:
  1670. return 32;
  1671. case AV_CODEC_ID_PCM_F64BE:
  1672. case AV_CODEC_ID_PCM_F64LE:
  1673. return 64;
  1674. default:
  1675. return 0;
  1676. }
  1677. }
  1678. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1679. {
  1680. switch (codec_id) {
  1681. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1682. return 2;
  1683. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1684. return 3;
  1685. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1686. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1687. case AV_CODEC_ID_ADPCM_IMA_QT:
  1688. case AV_CODEC_ID_ADPCM_SWF:
  1689. case AV_CODEC_ID_ADPCM_MS:
  1690. return 4;
  1691. default:
  1692. return av_get_exact_bits_per_sample(codec_id);
  1693. }
  1694. }
  1695. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1696. {
  1697. int id, sr, ch, ba, tag, bps;
  1698. id = avctx->codec_id;
  1699. sr = avctx->sample_rate;
  1700. ch = avctx->channels;
  1701. ba = avctx->block_align;
  1702. tag = avctx->codec_tag;
  1703. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1704. /* codecs with an exact constant bits per sample */
  1705. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1706. return (frame_bytes * 8) / (bps * ch);
  1707. bps = avctx->bits_per_coded_sample;
  1708. /* codecs with a fixed packet duration */
  1709. switch (id) {
  1710. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1711. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1712. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1713. case AV_CODEC_ID_AMR_NB:
  1714. case AV_CODEC_ID_GSM:
  1715. case AV_CODEC_ID_QCELP:
  1716. case AV_CODEC_ID_RA_144:
  1717. case AV_CODEC_ID_RA_288: return 160;
  1718. case AV_CODEC_ID_IMC: return 256;
  1719. case AV_CODEC_ID_AMR_WB:
  1720. case AV_CODEC_ID_GSM_MS: return 320;
  1721. case AV_CODEC_ID_MP1: return 384;
  1722. case AV_CODEC_ID_ATRAC1: return 512;
  1723. case AV_CODEC_ID_ATRAC3: return 1024;
  1724. case AV_CODEC_ID_MP2:
  1725. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1726. case AV_CODEC_ID_AC3: return 1536;
  1727. }
  1728. if (sr > 0) {
  1729. /* calc from sample rate */
  1730. if (id == AV_CODEC_ID_TTA)
  1731. return 256 * sr / 245;
  1732. if (ch > 0) {
  1733. /* calc from sample rate and channels */
  1734. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1735. return (480 << (sr / 22050)) / ch;
  1736. }
  1737. }
  1738. if (ba > 0) {
  1739. /* calc from block_align */
  1740. if (id == AV_CODEC_ID_SIPR) {
  1741. switch (ba) {
  1742. case 20: return 160;
  1743. case 19: return 144;
  1744. case 29: return 288;
  1745. case 37: return 480;
  1746. }
  1747. } else if (id == AV_CODEC_ID_ILBC) {
  1748. switch (ba) {
  1749. case 38: return 160;
  1750. case 50: return 240;
  1751. }
  1752. }
  1753. }
  1754. if (frame_bytes > 0) {
  1755. /* calc from frame_bytes only */
  1756. if (id == AV_CODEC_ID_TRUESPEECH)
  1757. return 240 * (frame_bytes / 32);
  1758. if (id == AV_CODEC_ID_NELLYMOSER)
  1759. return 256 * (frame_bytes / 64);
  1760. if (bps > 0) {
  1761. /* calc from frame_bytes and bits_per_coded_sample */
  1762. if (id == AV_CODEC_ID_ADPCM_G726)
  1763. return frame_bytes * 8 / bps;
  1764. }
  1765. if (ch > 0) {
  1766. /* calc from frame_bytes and channels */
  1767. switch (id) {
  1768. case AV_CODEC_ID_ADPCM_4XM:
  1769. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1770. return (frame_bytes - 4 * ch) * 2 / ch;
  1771. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1772. return (frame_bytes - 4) * 2 / ch;
  1773. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1774. return (frame_bytes - 8) * 2 / ch;
  1775. case AV_CODEC_ID_ADPCM_XA:
  1776. return (frame_bytes / 128) * 224 / ch;
  1777. case AV_CODEC_ID_INTERPLAY_DPCM:
  1778. return (frame_bytes - 6 - ch) / ch;
  1779. case AV_CODEC_ID_ROQ_DPCM:
  1780. return (frame_bytes - 8) / ch;
  1781. case AV_CODEC_ID_XAN_DPCM:
  1782. return (frame_bytes - 2 * ch) / ch;
  1783. case AV_CODEC_ID_MACE3:
  1784. return 3 * frame_bytes / ch;
  1785. case AV_CODEC_ID_MACE6:
  1786. return 6 * frame_bytes / ch;
  1787. case AV_CODEC_ID_PCM_LXF:
  1788. return 2 * (frame_bytes / (5 * ch));
  1789. }
  1790. if (tag) {
  1791. /* calc from frame_bytes, channels, and codec_tag */
  1792. if (id == AV_CODEC_ID_SOL_DPCM) {
  1793. if (tag == 3)
  1794. return frame_bytes / ch;
  1795. else
  1796. return frame_bytes * 2 / ch;
  1797. }
  1798. }
  1799. if (ba > 0) {
  1800. /* calc from frame_bytes, channels, and block_align */
  1801. int blocks = frame_bytes / ba;
  1802. switch (avctx->codec_id) {
  1803. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1804. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1805. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1806. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1807. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1808. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1809. case AV_CODEC_ID_ADPCM_MS:
  1810. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1811. }
  1812. }
  1813. if (bps > 0) {
  1814. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1815. switch (avctx->codec_id) {
  1816. case AV_CODEC_ID_PCM_DVD:
  1817. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1818. case AV_CODEC_ID_PCM_BLURAY:
  1819. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1820. case AV_CODEC_ID_S302M:
  1821. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1822. }
  1823. }
  1824. }
  1825. }
  1826. return 0;
  1827. }
  1828. #if !HAVE_THREADS
  1829. int ff_thread_init(AVCodecContext *s)
  1830. {
  1831. return -1;
  1832. }
  1833. #endif
  1834. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1835. {
  1836. unsigned int n = 0;
  1837. while (v >= 0xff) {
  1838. *s++ = 0xff;
  1839. v -= 0xff;
  1840. n++;
  1841. }
  1842. *s = v;
  1843. n++;
  1844. return n;
  1845. }
  1846. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1847. {
  1848. int i;
  1849. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1850. return i;
  1851. }
  1852. #if FF_API_MISSING_SAMPLE
  1853. FF_DISABLE_DEPRECATION_WARNINGS
  1854. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1855. {
  1856. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
  1857. "version to the newest one from Git. If the problem still "
  1858. "occurs, it means that your file has a feature which has not "
  1859. "been implemented.\n", feature);
  1860. if(want_sample)
  1861. av_log_ask_for_sample(avc, NULL);
  1862. }
  1863. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1864. {
  1865. va_list argument_list;
  1866. va_start(argument_list, msg);
  1867. if (msg)
  1868. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1869. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1870. "of this file to ftp://upload.libav.org/incoming/ "
  1871. "and contact the libav-devel mailing list.\n");
  1872. va_end(argument_list);
  1873. }
  1874. FF_ENABLE_DEPRECATION_WARNINGS
  1875. #endif /* FF_API_MISSING_SAMPLE */
  1876. static AVHWAccel *first_hwaccel = NULL;
  1877. void av_register_hwaccel(AVHWAccel *hwaccel)
  1878. {
  1879. AVHWAccel **p = &first_hwaccel;
  1880. while (*p)
  1881. p = &(*p)->next;
  1882. *p = hwaccel;
  1883. hwaccel->next = NULL;
  1884. }
  1885. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1886. {
  1887. return hwaccel ? hwaccel->next : first_hwaccel;
  1888. }
  1889. AVHWAccel *ff_find_hwaccel(AVCodecContext *avctx)
  1890. {
  1891. enum AVCodecID codec_id = avctx->codec->id;
  1892. enum AVPixelFormat pix_fmt = avctx->pix_fmt;
  1893. AVHWAccel *hwaccel = NULL;
  1894. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1895. if (hwaccel->id == codec_id
  1896. && hwaccel->pix_fmt == pix_fmt)
  1897. return hwaccel;
  1898. return NULL;
  1899. }
  1900. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1901. {
  1902. if (lockmgr_cb) {
  1903. if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1904. return -1;
  1905. if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1906. return -1;
  1907. }
  1908. lockmgr_cb = cb;
  1909. if (lockmgr_cb) {
  1910. if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1911. return -1;
  1912. if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1913. return -1;
  1914. }
  1915. return 0;
  1916. }
  1917. int avpriv_lock_avformat(void)
  1918. {
  1919. if (lockmgr_cb) {
  1920. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1921. return -1;
  1922. }
  1923. return 0;
  1924. }
  1925. int avpriv_unlock_avformat(void)
  1926. {
  1927. if (lockmgr_cb) {
  1928. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1929. return -1;
  1930. }
  1931. return 0;
  1932. }
  1933. unsigned int avpriv_toupper4(unsigned int x)
  1934. {
  1935. return av_toupper(x & 0xFF) +
  1936. (av_toupper((x >> 8) & 0xFF) << 8) +
  1937. (av_toupper((x >> 16) & 0xFF) << 16) +
  1938. (av_toupper((x >> 24) & 0xFF) << 24);
  1939. }
  1940. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  1941. {
  1942. int ret;
  1943. dst->owner = src->owner;
  1944. ret = av_frame_ref(dst->f, src->f);
  1945. if (ret < 0)
  1946. return ret;
  1947. if (src->progress &&
  1948. !(dst->progress = av_buffer_ref(src->progress))) {
  1949. ff_thread_release_buffer(dst->owner, dst);
  1950. return AVERROR(ENOMEM);
  1951. }
  1952. return 0;
  1953. }
  1954. #if !HAVE_THREADS
  1955. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  1956. {
  1957. f->owner = avctx;
  1958. return ff_get_buffer(avctx, f->f, flags);
  1959. }
  1960. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  1961. {
  1962. av_frame_unref(f->f);
  1963. }
  1964. void ff_thread_finish_setup(AVCodecContext *avctx)
  1965. {
  1966. }
  1967. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  1968. {
  1969. }
  1970. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  1971. {
  1972. }
  1973. #endif
  1974. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  1975. {
  1976. if (codec_id <= AV_CODEC_ID_NONE)
  1977. return AVMEDIA_TYPE_UNKNOWN;
  1978. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  1979. return AVMEDIA_TYPE_VIDEO;
  1980. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  1981. return AVMEDIA_TYPE_AUDIO;
  1982. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  1983. return AVMEDIA_TYPE_SUBTITLE;
  1984. return AVMEDIA_TYPE_UNKNOWN;
  1985. }
  1986. int avcodec_is_open(AVCodecContext *s)
  1987. {
  1988. return !!s->internal;
  1989. }
  1990. const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
  1991. const uint8_t *end,
  1992. uint32_t * restrict state)
  1993. {
  1994. int i;
  1995. assert(p <= end);
  1996. if (p >= end)
  1997. return end;
  1998. for (i = 0; i < 3; i++) {
  1999. uint32_t tmp = *state << 8;
  2000. *state = tmp + *(p++);
  2001. if (tmp == 0x100 || p == end)
  2002. return p;
  2003. }
  2004. while (p < end) {
  2005. if (p[-1] > 1 ) p += 3;
  2006. else if (p[-2] ) p += 2;
  2007. else if (p[-3]|(p[-1]-1)) p++;
  2008. else {
  2009. p++;
  2010. break;
  2011. }
  2012. }
  2013. p = FFMIN(p, end) - 4;
  2014. *state = AV_RB32(p);
  2015. return p + 4;
  2016. }