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.

2331 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. return 0;
  660. av_frame_move_ref(&tmp, frame);
  661. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  662. if (ret < 0) {
  663. av_frame_unref(&tmp);
  664. return ret;
  665. }
  666. av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
  667. frame->format, frame->width, frame->height);
  668. av_frame_unref(&tmp);
  669. return 0;
  670. }
  671. #if FF_API_GET_BUFFER
  672. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  673. {
  674. av_frame_unref(pic);
  675. }
  676. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  677. {
  678. av_assert0(0);
  679. return AVERROR_BUG;
  680. }
  681. #endif
  682. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  683. {
  684. int i;
  685. for (i = 0; i < count; i++) {
  686. int r = func(c, (char *)arg + i * size);
  687. if (ret)
  688. ret[i] = r;
  689. }
  690. return 0;
  691. }
  692. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  693. {
  694. int i;
  695. for (i = 0; i < count; i++) {
  696. int r = func(c, arg, i, 0);
  697. if (ret)
  698. ret[i] = r;
  699. }
  700. return 0;
  701. }
  702. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  703. {
  704. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  705. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  706. }
  707. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  708. {
  709. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  710. ++fmt;
  711. return fmt[0];
  712. }
  713. #if FF_API_AVFRAME_LAVC
  714. void avcodec_get_frame_defaults(AVFrame *frame)
  715. {
  716. if (frame->extended_data != frame->data)
  717. av_freep(&frame->extended_data);
  718. memset(frame, 0, sizeof(AVFrame));
  719. frame->pts = AV_NOPTS_VALUE;
  720. frame->key_frame = 1;
  721. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  722. frame->format = -1; /* unknown */
  723. frame->extended_data = frame->data;
  724. }
  725. AVFrame *avcodec_alloc_frame(void)
  726. {
  727. AVFrame *frame = av_mallocz(sizeof(AVFrame));
  728. if (frame == NULL)
  729. return NULL;
  730. FF_DISABLE_DEPRECATION_WARNINGS
  731. avcodec_get_frame_defaults(frame);
  732. FF_ENABLE_DEPRECATION_WARNINGS
  733. return frame;
  734. }
  735. void avcodec_free_frame(AVFrame **frame)
  736. {
  737. av_frame_free(frame);
  738. }
  739. #endif
  740. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  741. {
  742. int ret = 0;
  743. AVDictionary *tmp = NULL;
  744. if (avcodec_is_open(avctx))
  745. return 0;
  746. if ((!codec && !avctx->codec)) {
  747. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  748. return AVERROR(EINVAL);
  749. }
  750. if ((codec && avctx->codec && codec != avctx->codec)) {
  751. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  752. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  753. return AVERROR(EINVAL);
  754. }
  755. if (!codec)
  756. codec = avctx->codec;
  757. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  758. return AVERROR(EINVAL);
  759. if (options)
  760. av_dict_copy(&tmp, *options, 0);
  761. /* If there is a user-supplied mutex locking routine, call it. */
  762. if (lockmgr_cb) {
  763. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  764. return -1;
  765. }
  766. entangled_thread_counter++;
  767. if (entangled_thread_counter != 1) {
  768. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  769. ret = -1;
  770. goto end;
  771. }
  772. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  773. if (!avctx->internal) {
  774. ret = AVERROR(ENOMEM);
  775. goto end;
  776. }
  777. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  778. if (!avctx->internal->pool) {
  779. ret = AVERROR(ENOMEM);
  780. goto free_and_end;
  781. }
  782. avctx->internal->to_free = av_frame_alloc();
  783. if (!avctx->internal->to_free) {
  784. ret = AVERROR(ENOMEM);
  785. goto free_and_end;
  786. }
  787. if (codec->priv_data_size > 0) {
  788. if (!avctx->priv_data) {
  789. avctx->priv_data = av_mallocz(codec->priv_data_size);
  790. if (!avctx->priv_data) {
  791. ret = AVERROR(ENOMEM);
  792. goto end;
  793. }
  794. if (codec->priv_class) {
  795. *(const AVClass **)avctx->priv_data = codec->priv_class;
  796. av_opt_set_defaults(avctx->priv_data);
  797. }
  798. }
  799. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  800. goto free_and_end;
  801. } else {
  802. avctx->priv_data = NULL;
  803. }
  804. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  805. goto free_and_end;
  806. if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
  807. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  808. else if (avctx->width && avctx->height)
  809. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  810. if (ret < 0)
  811. goto free_and_end;
  812. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  813. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  814. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  815. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  816. ff_set_dimensions(avctx, 0, 0);
  817. }
  818. /* if the decoder init function was already called previously,
  819. * free the already allocated subtitle_header before overwriting it */
  820. if (av_codec_is_decoder(codec))
  821. av_freep(&avctx->subtitle_header);
  822. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  823. ret = AVERROR(EINVAL);
  824. goto free_and_end;
  825. }
  826. avctx->codec = codec;
  827. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  828. avctx->codec_id == AV_CODEC_ID_NONE) {
  829. avctx->codec_type = codec->type;
  830. avctx->codec_id = codec->id;
  831. }
  832. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  833. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  834. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  835. ret = AVERROR(EINVAL);
  836. goto free_and_end;
  837. }
  838. avctx->frame_number = 0;
  839. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  840. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  841. ret = AVERROR_EXPERIMENTAL;
  842. goto free_and_end;
  843. }
  844. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  845. (!avctx->time_base.num || !avctx->time_base.den)) {
  846. avctx->time_base.num = 1;
  847. avctx->time_base.den = avctx->sample_rate;
  848. }
  849. if (HAVE_THREADS) {
  850. ret = ff_thread_init(avctx);
  851. if (ret < 0) {
  852. goto free_and_end;
  853. }
  854. }
  855. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  856. avctx->thread_count = 1;
  857. if (av_codec_is_encoder(avctx->codec)) {
  858. int i;
  859. if (avctx->codec->sample_fmts) {
  860. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  861. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  862. break;
  863. if (avctx->channels == 1 &&
  864. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  865. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  866. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  867. break;
  868. }
  869. }
  870. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  871. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  872. ret = AVERROR(EINVAL);
  873. goto free_and_end;
  874. }
  875. }
  876. if (avctx->codec->pix_fmts) {
  877. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  878. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  879. break;
  880. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
  881. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  882. ret = AVERROR(EINVAL);
  883. goto free_and_end;
  884. }
  885. }
  886. if (avctx->codec->supported_samplerates) {
  887. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  888. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  889. break;
  890. if (avctx->codec->supported_samplerates[i] == 0) {
  891. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  892. ret = AVERROR(EINVAL);
  893. goto free_and_end;
  894. }
  895. }
  896. if (avctx->codec->channel_layouts) {
  897. if (!avctx->channel_layout) {
  898. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  899. } else {
  900. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  901. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  902. break;
  903. if (avctx->codec->channel_layouts[i] == 0) {
  904. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  905. ret = AVERROR(EINVAL);
  906. goto free_and_end;
  907. }
  908. }
  909. }
  910. if (avctx->channel_layout && avctx->channels) {
  911. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  912. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  913. ret = AVERROR(EINVAL);
  914. goto free_and_end;
  915. }
  916. } else if (avctx->channel_layout) {
  917. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  918. }
  919. if (!avctx->rc_initial_buffer_occupancy)
  920. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  921. }
  922. if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  923. ret = avctx->codec->init(avctx);
  924. if (ret < 0) {
  925. goto free_and_end;
  926. }
  927. }
  928. if (av_codec_is_decoder(avctx->codec)) {
  929. /* validate channel layout from the decoder */
  930. if (avctx->channel_layout) {
  931. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  932. if (!avctx->channels)
  933. avctx->channels = channels;
  934. else if (channels != avctx->channels) {
  935. av_log(avctx, AV_LOG_WARNING,
  936. "channel layout does not match number of channels\n");
  937. avctx->channel_layout = 0;
  938. }
  939. }
  940. if (avctx->channels && avctx->channels < 0 ||
  941. avctx->channels > FF_SANE_NB_CHANNELS) {
  942. ret = AVERROR(EINVAL);
  943. goto free_and_end;
  944. }
  945. }
  946. end:
  947. entangled_thread_counter--;
  948. /* Release any user-supplied mutex. */
  949. if (lockmgr_cb) {
  950. (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  951. }
  952. if (options) {
  953. av_dict_free(options);
  954. *options = tmp;
  955. }
  956. return ret;
  957. free_and_end:
  958. av_dict_free(&tmp);
  959. av_freep(&avctx->priv_data);
  960. if (avctx->internal) {
  961. av_frame_free(&avctx->internal->to_free);
  962. av_freep(&avctx->internal->pool);
  963. }
  964. av_freep(&avctx->internal);
  965. avctx->codec = NULL;
  966. goto end;
  967. }
  968. int ff_alloc_packet(AVPacket *avpkt, int size)
  969. {
  970. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  971. return AVERROR(EINVAL);
  972. if (avpkt->data) {
  973. AVBufferRef *buf = avpkt->buf;
  974. #if FF_API_DESTRUCT_PACKET
  975. FF_DISABLE_DEPRECATION_WARNINGS
  976. void *destruct = avpkt->destruct;
  977. FF_ENABLE_DEPRECATION_WARNINGS
  978. #endif
  979. if (avpkt->size < size)
  980. return AVERROR(EINVAL);
  981. av_init_packet(avpkt);
  982. #if FF_API_DESTRUCT_PACKET
  983. FF_DISABLE_DEPRECATION_WARNINGS
  984. avpkt->destruct = destruct;
  985. FF_ENABLE_DEPRECATION_WARNINGS
  986. #endif
  987. avpkt->buf = buf;
  988. avpkt->size = size;
  989. return 0;
  990. } else {
  991. return av_new_packet(avpkt, size);
  992. }
  993. }
  994. /**
  995. * Pad last frame with silence.
  996. */
  997. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  998. {
  999. AVFrame *frame = NULL;
  1000. int ret;
  1001. if (!(frame = av_frame_alloc()))
  1002. return AVERROR(ENOMEM);
  1003. frame->format = src->format;
  1004. frame->channel_layout = src->channel_layout;
  1005. frame->nb_samples = s->frame_size;
  1006. ret = av_frame_get_buffer(frame, 32);
  1007. if (ret < 0)
  1008. goto fail;
  1009. ret = av_frame_copy_props(frame, src);
  1010. if (ret < 0)
  1011. goto fail;
  1012. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1013. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1014. goto fail;
  1015. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1016. frame->nb_samples - src->nb_samples,
  1017. s->channels, s->sample_fmt)) < 0)
  1018. goto fail;
  1019. *dst = frame;
  1020. return 0;
  1021. fail:
  1022. av_frame_free(&frame);
  1023. return ret;
  1024. }
  1025. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1026. AVPacket *avpkt,
  1027. const AVFrame *frame,
  1028. int *got_packet_ptr)
  1029. {
  1030. AVFrame tmp;
  1031. AVFrame *padded_frame = NULL;
  1032. int ret;
  1033. int user_packet = !!avpkt->data;
  1034. *got_packet_ptr = 0;
  1035. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1036. av_free_packet(avpkt);
  1037. av_init_packet(avpkt);
  1038. return 0;
  1039. }
  1040. /* ensure that extended_data is properly set */
  1041. if (frame && !frame->extended_data) {
  1042. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1043. avctx->channels > AV_NUM_DATA_POINTERS) {
  1044. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1045. "with more than %d channels, but extended_data is not set.\n",
  1046. AV_NUM_DATA_POINTERS);
  1047. return AVERROR(EINVAL);
  1048. }
  1049. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1050. tmp = *frame;
  1051. tmp.extended_data = tmp.data;
  1052. frame = &tmp;
  1053. }
  1054. /* check for valid frame size */
  1055. if (frame) {
  1056. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1057. if (frame->nb_samples > avctx->frame_size)
  1058. return AVERROR(EINVAL);
  1059. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1060. if (frame->nb_samples < avctx->frame_size &&
  1061. !avctx->internal->last_audio_frame) {
  1062. ret = pad_last_frame(avctx, &padded_frame, frame);
  1063. if (ret < 0)
  1064. return ret;
  1065. frame = padded_frame;
  1066. avctx->internal->last_audio_frame = 1;
  1067. }
  1068. if (frame->nb_samples != avctx->frame_size) {
  1069. ret = AVERROR(EINVAL);
  1070. goto end;
  1071. }
  1072. }
  1073. }
  1074. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1075. if (!ret) {
  1076. if (*got_packet_ptr) {
  1077. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1078. if (avpkt->pts == AV_NOPTS_VALUE)
  1079. avpkt->pts = frame->pts;
  1080. if (!avpkt->duration)
  1081. avpkt->duration = ff_samples_to_time_base(avctx,
  1082. frame->nb_samples);
  1083. }
  1084. avpkt->dts = avpkt->pts;
  1085. } else {
  1086. avpkt->size = 0;
  1087. }
  1088. if (!user_packet && avpkt->size) {
  1089. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  1090. if (ret >= 0)
  1091. avpkt->data = avpkt->buf->data;
  1092. }
  1093. avctx->frame_number++;
  1094. }
  1095. if (ret < 0 || !*got_packet_ptr) {
  1096. av_free_packet(avpkt);
  1097. av_init_packet(avpkt);
  1098. goto end;
  1099. }
  1100. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1101. * this needs to be moved to the encoders, but for now we can do it
  1102. * here to simplify things */
  1103. avpkt->flags |= AV_PKT_FLAG_KEY;
  1104. end:
  1105. av_frame_free(&padded_frame);
  1106. return ret;
  1107. }
  1108. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1109. AVPacket *avpkt,
  1110. const AVFrame *frame,
  1111. int *got_packet_ptr)
  1112. {
  1113. int ret;
  1114. int user_packet = !!avpkt->data;
  1115. *got_packet_ptr = 0;
  1116. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1117. av_free_packet(avpkt);
  1118. av_init_packet(avpkt);
  1119. avpkt->size = 0;
  1120. return 0;
  1121. }
  1122. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1123. return AVERROR(EINVAL);
  1124. av_assert0(avctx->codec->encode2);
  1125. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1126. if (!ret) {
  1127. if (!*got_packet_ptr)
  1128. avpkt->size = 0;
  1129. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1130. avpkt->pts = avpkt->dts = frame->pts;
  1131. if (!user_packet && avpkt->size) {
  1132. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  1133. if (ret >= 0)
  1134. avpkt->data = avpkt->buf->data;
  1135. }
  1136. avctx->frame_number++;
  1137. }
  1138. if (ret < 0 || !*got_packet_ptr)
  1139. av_free_packet(avpkt);
  1140. emms_c();
  1141. return ret;
  1142. }
  1143. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1144. const AVSubtitle *sub)
  1145. {
  1146. int ret;
  1147. if (sub->start_display_time) {
  1148. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1149. return -1;
  1150. }
  1151. if (sub->num_rects == 0 || !sub->rects)
  1152. return -1;
  1153. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1154. avctx->frame_number++;
  1155. return ret;
  1156. }
  1157. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1158. {
  1159. int size = 0, ret;
  1160. const uint8_t *data;
  1161. uint32_t flags;
  1162. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1163. if (!data)
  1164. return 0;
  1165. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
  1166. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  1167. "changes, but PARAM_CHANGE side data was sent to it.\n");
  1168. return AVERROR(EINVAL);
  1169. }
  1170. if (size < 4)
  1171. goto fail;
  1172. flags = bytestream_get_le32(&data);
  1173. size -= 4;
  1174. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1175. if (size < 4)
  1176. goto fail;
  1177. avctx->channels = bytestream_get_le32(&data);
  1178. size -= 4;
  1179. }
  1180. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1181. if (size < 8)
  1182. goto fail;
  1183. avctx->channel_layout = bytestream_get_le64(&data);
  1184. size -= 8;
  1185. }
  1186. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1187. if (size < 4)
  1188. goto fail;
  1189. avctx->sample_rate = bytestream_get_le32(&data);
  1190. size -= 4;
  1191. }
  1192. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1193. if (size < 8)
  1194. goto fail;
  1195. avctx->width = bytestream_get_le32(&data);
  1196. avctx->height = bytestream_get_le32(&data);
  1197. size -= 8;
  1198. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1199. if (ret < 0)
  1200. return ret;
  1201. }
  1202. return 0;
  1203. fail:
  1204. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  1205. return AVERROR_INVALIDDATA;
  1206. }
  1207. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  1208. {
  1209. int ret;
  1210. /* move the original frame to our backup */
  1211. av_frame_unref(avci->to_free);
  1212. av_frame_move_ref(avci->to_free, frame);
  1213. /* now copy everything except the AVBufferRefs back
  1214. * note that we make a COPY of the side data, so calling av_frame_free() on
  1215. * the caller's frame will work properly */
  1216. ret = av_frame_copy_props(frame, avci->to_free);
  1217. if (ret < 0)
  1218. return ret;
  1219. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  1220. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  1221. if (avci->to_free->extended_data != avci->to_free->data) {
  1222. int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
  1223. int size = planes * sizeof(*frame->extended_data);
  1224. if (!size) {
  1225. av_frame_unref(frame);
  1226. return AVERROR_BUG;
  1227. }
  1228. frame->extended_data = av_malloc(size);
  1229. if (!frame->extended_data) {
  1230. av_frame_unref(frame);
  1231. return AVERROR(ENOMEM);
  1232. }
  1233. memcpy(frame->extended_data, avci->to_free->extended_data,
  1234. size);
  1235. } else
  1236. frame->extended_data = frame->data;
  1237. frame->format = avci->to_free->format;
  1238. frame->width = avci->to_free->width;
  1239. frame->height = avci->to_free->height;
  1240. frame->channel_layout = avci->to_free->channel_layout;
  1241. frame->nb_samples = avci->to_free->nb_samples;
  1242. return 0;
  1243. }
  1244. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1245. int *got_picture_ptr,
  1246. AVPacket *avpkt)
  1247. {
  1248. AVCodecInternal *avci = avctx->internal;
  1249. int ret;
  1250. *got_picture_ptr = 0;
  1251. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1252. return -1;
  1253. avctx->internal->pkt = avpkt;
  1254. ret = apply_param_change(avctx, avpkt);
  1255. if (ret < 0) {
  1256. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1257. if (avctx->err_recognition & AV_EF_EXPLODE)
  1258. return ret;
  1259. }
  1260. av_frame_unref(picture);
  1261. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1262. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1263. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1264. avpkt);
  1265. else {
  1266. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1267. avpkt);
  1268. picture->pkt_dts = avpkt->dts;
  1269. /* get_buffer is supposed to set frame parameters */
  1270. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1271. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1272. picture->width = avctx->width;
  1273. picture->height = avctx->height;
  1274. picture->format = avctx->pix_fmt;
  1275. }
  1276. }
  1277. emms_c(); //needed to avoid an emms_c() call before every return;
  1278. if (*got_picture_ptr) {
  1279. if (!avctx->refcounted_frames) {
  1280. int err = unrefcount_frame(avci, picture);
  1281. if (err < 0)
  1282. return err;
  1283. }
  1284. avctx->frame_number++;
  1285. } else
  1286. av_frame_unref(picture);
  1287. } else
  1288. ret = 0;
  1289. return ret;
  1290. }
  1291. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1292. AVFrame *frame,
  1293. int *got_frame_ptr,
  1294. AVPacket *avpkt)
  1295. {
  1296. AVCodecInternal *avci = avctx->internal;
  1297. int ret = 0;
  1298. *got_frame_ptr = 0;
  1299. avctx->internal->pkt = avpkt;
  1300. if (!avpkt->data && avpkt->size) {
  1301. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1302. return AVERROR(EINVAL);
  1303. }
  1304. ret = apply_param_change(avctx, avpkt);
  1305. if (ret < 0) {
  1306. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1307. if (avctx->err_recognition & AV_EF_EXPLODE)
  1308. return ret;
  1309. }
  1310. av_frame_unref(frame);
  1311. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1312. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1313. if (ret >= 0 && *got_frame_ptr) {
  1314. avctx->frame_number++;
  1315. frame->pkt_dts = avpkt->dts;
  1316. if (frame->format == AV_SAMPLE_FMT_NONE)
  1317. frame->format = avctx->sample_fmt;
  1318. if (!avctx->refcounted_frames) {
  1319. int err = unrefcount_frame(avci, frame);
  1320. if (err < 0)
  1321. return err;
  1322. }
  1323. } else
  1324. av_frame_unref(frame);
  1325. }
  1326. return ret;
  1327. }
  1328. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1329. int *got_sub_ptr,
  1330. AVPacket *avpkt)
  1331. {
  1332. int ret;
  1333. avctx->internal->pkt = avpkt;
  1334. *got_sub_ptr = 0;
  1335. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1336. if (*got_sub_ptr)
  1337. avctx->frame_number++;
  1338. return ret;
  1339. }
  1340. void avsubtitle_free(AVSubtitle *sub)
  1341. {
  1342. int i;
  1343. for (i = 0; i < sub->num_rects; i++) {
  1344. av_freep(&sub->rects[i]->pict.data[0]);
  1345. av_freep(&sub->rects[i]->pict.data[1]);
  1346. av_freep(&sub->rects[i]->pict.data[2]);
  1347. av_freep(&sub->rects[i]->pict.data[3]);
  1348. av_freep(&sub->rects[i]->text);
  1349. av_freep(&sub->rects[i]->ass);
  1350. av_freep(&sub->rects[i]);
  1351. }
  1352. av_freep(&sub->rects);
  1353. memset(sub, 0, sizeof(AVSubtitle));
  1354. }
  1355. av_cold int avcodec_close(AVCodecContext *avctx)
  1356. {
  1357. /* If there is a user-supplied mutex locking routine, call it. */
  1358. if (lockmgr_cb) {
  1359. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1360. return -1;
  1361. }
  1362. entangled_thread_counter++;
  1363. if (entangled_thread_counter != 1) {
  1364. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1365. entangled_thread_counter--;
  1366. return -1;
  1367. }
  1368. if (avcodec_is_open(avctx)) {
  1369. FramePool *pool = avctx->internal->pool;
  1370. int i;
  1371. if (HAVE_THREADS && avctx->internal->thread_ctx)
  1372. ff_thread_free(avctx);
  1373. if (avctx->codec && avctx->codec->close)
  1374. avctx->codec->close(avctx);
  1375. avctx->coded_frame = NULL;
  1376. av_frame_free(&avctx->internal->to_free);
  1377. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  1378. av_buffer_pool_uninit(&pool->pools[i]);
  1379. av_freep(&avctx->internal->pool);
  1380. av_freep(&avctx->internal);
  1381. }
  1382. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1383. av_opt_free(avctx->priv_data);
  1384. av_opt_free(avctx);
  1385. av_freep(&avctx->priv_data);
  1386. if (av_codec_is_encoder(avctx->codec))
  1387. av_freep(&avctx->extradata);
  1388. avctx->codec = NULL;
  1389. avctx->active_thread_type = 0;
  1390. entangled_thread_counter--;
  1391. /* Release any user-supplied mutex. */
  1392. if (lockmgr_cb) {
  1393. (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1394. }
  1395. return 0;
  1396. }
  1397. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1398. {
  1399. AVCodec *p, *experimental = NULL;
  1400. p = first_avcodec;
  1401. while (p) {
  1402. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1403. p->id == id) {
  1404. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1405. experimental = p;
  1406. } else
  1407. return p;
  1408. }
  1409. p = p->next;
  1410. }
  1411. return experimental;
  1412. }
  1413. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1414. {
  1415. return find_encdec(id, 1);
  1416. }
  1417. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1418. {
  1419. AVCodec *p;
  1420. if (!name)
  1421. return NULL;
  1422. p = first_avcodec;
  1423. while (p) {
  1424. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1425. return p;
  1426. p = p->next;
  1427. }
  1428. return NULL;
  1429. }
  1430. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1431. {
  1432. return find_encdec(id, 0);
  1433. }
  1434. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1435. {
  1436. AVCodec *p;
  1437. if (!name)
  1438. return NULL;
  1439. p = first_avcodec;
  1440. while (p) {
  1441. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1442. return p;
  1443. p = p->next;
  1444. }
  1445. return NULL;
  1446. }
  1447. static int get_bit_rate(AVCodecContext *ctx)
  1448. {
  1449. int bit_rate;
  1450. int bits_per_sample;
  1451. switch (ctx->codec_type) {
  1452. case AVMEDIA_TYPE_VIDEO:
  1453. case AVMEDIA_TYPE_DATA:
  1454. case AVMEDIA_TYPE_SUBTITLE:
  1455. case AVMEDIA_TYPE_ATTACHMENT:
  1456. bit_rate = ctx->bit_rate;
  1457. break;
  1458. case AVMEDIA_TYPE_AUDIO:
  1459. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1460. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1461. break;
  1462. default:
  1463. bit_rate = 0;
  1464. break;
  1465. }
  1466. return bit_rate;
  1467. }
  1468. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1469. {
  1470. int i, len, ret = 0;
  1471. #define TAG_PRINT(x) \
  1472. (((x) >= '0' && (x) <= '9') || \
  1473. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1474. ((x) == '.' || (x) == ' '))
  1475. for (i = 0; i < 4; i++) {
  1476. len = snprintf(buf, buf_size,
  1477. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1478. buf += len;
  1479. buf_size = buf_size > len ? buf_size - len : 0;
  1480. ret += len;
  1481. codec_tag >>= 8;
  1482. }
  1483. return ret;
  1484. }
  1485. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1486. {
  1487. const char *codec_name;
  1488. const char *profile = NULL;
  1489. const AVCodec *p;
  1490. char buf1[32];
  1491. int bitrate;
  1492. AVRational display_aspect_ratio;
  1493. if (enc->codec)
  1494. p = enc->codec;
  1495. else if (encode)
  1496. p = avcodec_find_encoder(enc->codec_id);
  1497. else
  1498. p = avcodec_find_decoder(enc->codec_id);
  1499. if (p) {
  1500. codec_name = p->name;
  1501. profile = av_get_profile_name(p, enc->profile);
  1502. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1503. /* fake mpeg2 transport stream codec (currently not
  1504. * registered) */
  1505. codec_name = "mpeg2ts";
  1506. } else if (enc->codec_name[0] != '\0') {
  1507. codec_name = enc->codec_name;
  1508. } else {
  1509. /* output avi tags */
  1510. char tag_buf[32];
  1511. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1512. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1513. codec_name = buf1;
  1514. }
  1515. switch (enc->codec_type) {
  1516. case AVMEDIA_TYPE_VIDEO:
  1517. snprintf(buf, buf_size,
  1518. "Video: %s%s",
  1519. codec_name, enc->mb_decision ? " (hq)" : "");
  1520. if (profile)
  1521. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1522. " (%s)", profile);
  1523. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1524. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1525. ", %s",
  1526. av_get_pix_fmt_name(enc->pix_fmt));
  1527. }
  1528. if (enc->width) {
  1529. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1530. ", %dx%d",
  1531. enc->width, enc->height);
  1532. if (enc->sample_aspect_ratio.num) {
  1533. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1534. enc->width * enc->sample_aspect_ratio.num,
  1535. enc->height * enc->sample_aspect_ratio.den,
  1536. 1024 * 1024);
  1537. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1538. " [PAR %d:%d DAR %d:%d]",
  1539. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1540. display_aspect_ratio.num, display_aspect_ratio.den);
  1541. }
  1542. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1543. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1544. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1545. ", %d/%d",
  1546. enc->time_base.num / g, enc->time_base.den / g);
  1547. }
  1548. }
  1549. if (encode) {
  1550. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1551. ", q=%d-%d", enc->qmin, enc->qmax);
  1552. }
  1553. break;
  1554. case AVMEDIA_TYPE_AUDIO:
  1555. snprintf(buf, buf_size,
  1556. "Audio: %s",
  1557. codec_name);
  1558. if (profile)
  1559. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1560. " (%s)", profile);
  1561. if (enc->sample_rate) {
  1562. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1563. ", %d Hz", enc->sample_rate);
  1564. }
  1565. av_strlcat(buf, ", ", buf_size);
  1566. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1567. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1568. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1569. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1570. }
  1571. break;
  1572. case AVMEDIA_TYPE_DATA:
  1573. snprintf(buf, buf_size, "Data: %s", codec_name);
  1574. break;
  1575. case AVMEDIA_TYPE_SUBTITLE:
  1576. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1577. break;
  1578. case AVMEDIA_TYPE_ATTACHMENT:
  1579. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1580. break;
  1581. default:
  1582. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1583. return;
  1584. }
  1585. if (encode) {
  1586. if (enc->flags & CODEC_FLAG_PASS1)
  1587. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1588. ", pass 1");
  1589. if (enc->flags & CODEC_FLAG_PASS2)
  1590. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1591. ", pass 2");
  1592. }
  1593. bitrate = get_bit_rate(enc);
  1594. if (bitrate != 0) {
  1595. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1596. ", %d kb/s", bitrate / 1000);
  1597. }
  1598. }
  1599. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1600. {
  1601. const AVProfile *p;
  1602. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1603. return NULL;
  1604. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1605. if (p->profile == profile)
  1606. return p->name;
  1607. return NULL;
  1608. }
  1609. unsigned avcodec_version(void)
  1610. {
  1611. return LIBAVCODEC_VERSION_INT;
  1612. }
  1613. const char *avcodec_configuration(void)
  1614. {
  1615. return LIBAV_CONFIGURATION;
  1616. }
  1617. const char *avcodec_license(void)
  1618. {
  1619. #define LICENSE_PREFIX "libavcodec license: "
  1620. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1621. }
  1622. void avcodec_flush_buffers(AVCodecContext *avctx)
  1623. {
  1624. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1625. ff_thread_flush(avctx);
  1626. else if (avctx->codec->flush)
  1627. avctx->codec->flush(avctx);
  1628. if (!avctx->refcounted_frames)
  1629. av_frame_unref(avctx->internal->to_free);
  1630. }
  1631. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1632. {
  1633. switch (codec_id) {
  1634. case AV_CODEC_ID_ADPCM_CT:
  1635. case AV_CODEC_ID_ADPCM_IMA_APC:
  1636. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1637. case AV_CODEC_ID_ADPCM_IMA_WS:
  1638. case AV_CODEC_ID_ADPCM_G722:
  1639. case AV_CODEC_ID_ADPCM_YAMAHA:
  1640. return 4;
  1641. case AV_CODEC_ID_PCM_ALAW:
  1642. case AV_CODEC_ID_PCM_MULAW:
  1643. case AV_CODEC_ID_PCM_S8:
  1644. case AV_CODEC_ID_PCM_U8:
  1645. case AV_CODEC_ID_PCM_ZORK:
  1646. return 8;
  1647. case AV_CODEC_ID_PCM_S16BE:
  1648. case AV_CODEC_ID_PCM_S16LE:
  1649. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1650. case AV_CODEC_ID_PCM_U16BE:
  1651. case AV_CODEC_ID_PCM_U16LE:
  1652. return 16;
  1653. case AV_CODEC_ID_PCM_S24DAUD:
  1654. case AV_CODEC_ID_PCM_S24BE:
  1655. case AV_CODEC_ID_PCM_S24LE:
  1656. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  1657. case AV_CODEC_ID_PCM_U24BE:
  1658. case AV_CODEC_ID_PCM_U24LE:
  1659. return 24;
  1660. case AV_CODEC_ID_PCM_S32BE:
  1661. case AV_CODEC_ID_PCM_S32LE:
  1662. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  1663. case AV_CODEC_ID_PCM_U32BE:
  1664. case AV_CODEC_ID_PCM_U32LE:
  1665. case AV_CODEC_ID_PCM_F32BE:
  1666. case AV_CODEC_ID_PCM_F32LE:
  1667. return 32;
  1668. case AV_CODEC_ID_PCM_F64BE:
  1669. case AV_CODEC_ID_PCM_F64LE:
  1670. return 64;
  1671. default:
  1672. return 0;
  1673. }
  1674. }
  1675. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1676. {
  1677. switch (codec_id) {
  1678. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1679. return 2;
  1680. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1681. return 3;
  1682. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1683. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1684. case AV_CODEC_ID_ADPCM_IMA_QT:
  1685. case AV_CODEC_ID_ADPCM_SWF:
  1686. case AV_CODEC_ID_ADPCM_MS:
  1687. return 4;
  1688. default:
  1689. return av_get_exact_bits_per_sample(codec_id);
  1690. }
  1691. }
  1692. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1693. {
  1694. int id, sr, ch, ba, tag, bps;
  1695. id = avctx->codec_id;
  1696. sr = avctx->sample_rate;
  1697. ch = avctx->channels;
  1698. ba = avctx->block_align;
  1699. tag = avctx->codec_tag;
  1700. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1701. /* codecs with an exact constant bits per sample */
  1702. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1703. return (frame_bytes * 8) / (bps * ch);
  1704. bps = avctx->bits_per_coded_sample;
  1705. /* codecs with a fixed packet duration */
  1706. switch (id) {
  1707. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1708. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1709. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1710. case AV_CODEC_ID_AMR_NB:
  1711. case AV_CODEC_ID_GSM:
  1712. case AV_CODEC_ID_QCELP:
  1713. case AV_CODEC_ID_RA_144:
  1714. case AV_CODEC_ID_RA_288: return 160;
  1715. case AV_CODEC_ID_IMC: return 256;
  1716. case AV_CODEC_ID_AMR_WB:
  1717. case AV_CODEC_ID_GSM_MS: return 320;
  1718. case AV_CODEC_ID_MP1: return 384;
  1719. case AV_CODEC_ID_ATRAC1: return 512;
  1720. case AV_CODEC_ID_ATRAC3: return 1024;
  1721. case AV_CODEC_ID_MP2:
  1722. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1723. case AV_CODEC_ID_AC3: return 1536;
  1724. }
  1725. if (sr > 0) {
  1726. /* calc from sample rate */
  1727. if (id == AV_CODEC_ID_TTA)
  1728. return 256 * sr / 245;
  1729. if (ch > 0) {
  1730. /* calc from sample rate and channels */
  1731. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1732. return (480 << (sr / 22050)) / ch;
  1733. }
  1734. }
  1735. if (ba > 0) {
  1736. /* calc from block_align */
  1737. if (id == AV_CODEC_ID_SIPR) {
  1738. switch (ba) {
  1739. case 20: return 160;
  1740. case 19: return 144;
  1741. case 29: return 288;
  1742. case 37: return 480;
  1743. }
  1744. } else if (id == AV_CODEC_ID_ILBC) {
  1745. switch (ba) {
  1746. case 38: return 160;
  1747. case 50: return 240;
  1748. }
  1749. }
  1750. }
  1751. if (frame_bytes > 0) {
  1752. /* calc from frame_bytes only */
  1753. if (id == AV_CODEC_ID_TRUESPEECH)
  1754. return 240 * (frame_bytes / 32);
  1755. if (id == AV_CODEC_ID_NELLYMOSER)
  1756. return 256 * (frame_bytes / 64);
  1757. if (bps > 0) {
  1758. /* calc from frame_bytes and bits_per_coded_sample */
  1759. if (id == AV_CODEC_ID_ADPCM_G726)
  1760. return frame_bytes * 8 / bps;
  1761. }
  1762. if (ch > 0) {
  1763. /* calc from frame_bytes and channels */
  1764. switch (id) {
  1765. case AV_CODEC_ID_ADPCM_4XM:
  1766. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1767. return (frame_bytes - 4 * ch) * 2 / ch;
  1768. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1769. return (frame_bytes - 4) * 2 / ch;
  1770. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1771. return (frame_bytes - 8) * 2 / ch;
  1772. case AV_CODEC_ID_ADPCM_XA:
  1773. return (frame_bytes / 128) * 224 / ch;
  1774. case AV_CODEC_ID_INTERPLAY_DPCM:
  1775. return (frame_bytes - 6 - ch) / ch;
  1776. case AV_CODEC_ID_ROQ_DPCM:
  1777. return (frame_bytes - 8) / ch;
  1778. case AV_CODEC_ID_XAN_DPCM:
  1779. return (frame_bytes - 2 * ch) / ch;
  1780. case AV_CODEC_ID_MACE3:
  1781. return 3 * frame_bytes / ch;
  1782. case AV_CODEC_ID_MACE6:
  1783. return 6 * frame_bytes / ch;
  1784. case AV_CODEC_ID_PCM_LXF:
  1785. return 2 * (frame_bytes / (5 * ch));
  1786. }
  1787. if (tag) {
  1788. /* calc from frame_bytes, channels, and codec_tag */
  1789. if (id == AV_CODEC_ID_SOL_DPCM) {
  1790. if (tag == 3)
  1791. return frame_bytes / ch;
  1792. else
  1793. return frame_bytes * 2 / ch;
  1794. }
  1795. }
  1796. if (ba > 0) {
  1797. /* calc from frame_bytes, channels, and block_align */
  1798. int blocks = frame_bytes / ba;
  1799. switch (avctx->codec_id) {
  1800. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1801. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1802. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1803. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1804. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1805. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1806. case AV_CODEC_ID_ADPCM_MS:
  1807. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1808. }
  1809. }
  1810. if (bps > 0) {
  1811. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1812. switch (avctx->codec_id) {
  1813. case AV_CODEC_ID_PCM_DVD:
  1814. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1815. case AV_CODEC_ID_PCM_BLURAY:
  1816. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1817. case AV_CODEC_ID_S302M:
  1818. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1819. }
  1820. }
  1821. }
  1822. }
  1823. return 0;
  1824. }
  1825. #if !HAVE_THREADS
  1826. int ff_thread_init(AVCodecContext *s)
  1827. {
  1828. return -1;
  1829. }
  1830. #endif
  1831. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1832. {
  1833. unsigned int n = 0;
  1834. while (v >= 0xff) {
  1835. *s++ = 0xff;
  1836. v -= 0xff;
  1837. n++;
  1838. }
  1839. *s = v;
  1840. n++;
  1841. return n;
  1842. }
  1843. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1844. {
  1845. int i;
  1846. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1847. return i;
  1848. }
  1849. #if FF_API_MISSING_SAMPLE
  1850. FF_DISABLE_DEPRECATION_WARNINGS
  1851. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1852. {
  1853. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
  1854. "version to the newest one from Git. If the problem still "
  1855. "occurs, it means that your file has a feature which has not "
  1856. "been implemented.\n", feature);
  1857. if(want_sample)
  1858. av_log_ask_for_sample(avc, NULL);
  1859. }
  1860. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1861. {
  1862. va_list argument_list;
  1863. va_start(argument_list, msg);
  1864. if (msg)
  1865. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1866. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1867. "of this file to ftp://upload.libav.org/incoming/ "
  1868. "and contact the libav-devel mailing list.\n");
  1869. va_end(argument_list);
  1870. }
  1871. FF_ENABLE_DEPRECATION_WARNINGS
  1872. #endif /* FF_API_MISSING_SAMPLE */
  1873. static AVHWAccel *first_hwaccel = NULL;
  1874. void av_register_hwaccel(AVHWAccel *hwaccel)
  1875. {
  1876. AVHWAccel **p = &first_hwaccel;
  1877. while (*p)
  1878. p = &(*p)->next;
  1879. *p = hwaccel;
  1880. hwaccel->next = NULL;
  1881. }
  1882. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1883. {
  1884. return hwaccel ? hwaccel->next : first_hwaccel;
  1885. }
  1886. AVHWAccel *ff_find_hwaccel(AVCodecContext *avctx)
  1887. {
  1888. enum AVCodecID codec_id = avctx->codec->id;
  1889. enum AVPixelFormat pix_fmt = avctx->pix_fmt;
  1890. AVHWAccel *hwaccel = NULL;
  1891. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1892. if (hwaccel->id == codec_id
  1893. && hwaccel->pix_fmt == pix_fmt)
  1894. return hwaccel;
  1895. return NULL;
  1896. }
  1897. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1898. {
  1899. if (lockmgr_cb) {
  1900. if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1901. return -1;
  1902. if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1903. return -1;
  1904. }
  1905. lockmgr_cb = cb;
  1906. if (lockmgr_cb) {
  1907. if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1908. return -1;
  1909. if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1910. return -1;
  1911. }
  1912. return 0;
  1913. }
  1914. int avpriv_lock_avformat(void)
  1915. {
  1916. if (lockmgr_cb) {
  1917. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1918. return -1;
  1919. }
  1920. return 0;
  1921. }
  1922. int avpriv_unlock_avformat(void)
  1923. {
  1924. if (lockmgr_cb) {
  1925. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1926. return -1;
  1927. }
  1928. return 0;
  1929. }
  1930. unsigned int avpriv_toupper4(unsigned int x)
  1931. {
  1932. return av_toupper(x & 0xFF) +
  1933. (av_toupper((x >> 8) & 0xFF) << 8) +
  1934. (av_toupper((x >> 16) & 0xFF) << 16) +
  1935. (av_toupper((x >> 24) & 0xFF) << 24);
  1936. }
  1937. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  1938. {
  1939. int ret;
  1940. dst->owner = src->owner;
  1941. ret = av_frame_ref(dst->f, src->f);
  1942. if (ret < 0)
  1943. return ret;
  1944. if (src->progress &&
  1945. !(dst->progress = av_buffer_ref(src->progress))) {
  1946. ff_thread_release_buffer(dst->owner, dst);
  1947. return AVERROR(ENOMEM);
  1948. }
  1949. return 0;
  1950. }
  1951. #if !HAVE_THREADS
  1952. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  1953. {
  1954. f->owner = avctx;
  1955. return ff_get_buffer(avctx, f->f, flags);
  1956. }
  1957. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  1958. {
  1959. av_frame_unref(f->f);
  1960. }
  1961. void ff_thread_finish_setup(AVCodecContext *avctx)
  1962. {
  1963. }
  1964. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  1965. {
  1966. }
  1967. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  1968. {
  1969. }
  1970. #endif
  1971. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  1972. {
  1973. if (codec_id <= AV_CODEC_ID_NONE)
  1974. return AVMEDIA_TYPE_UNKNOWN;
  1975. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  1976. return AVMEDIA_TYPE_VIDEO;
  1977. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  1978. return AVMEDIA_TYPE_AUDIO;
  1979. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  1980. return AVMEDIA_TYPE_SUBTITLE;
  1981. return AVMEDIA_TYPE_UNKNOWN;
  1982. }
  1983. int avcodec_is_open(AVCodecContext *s)
  1984. {
  1985. return !!s->internal;
  1986. }
  1987. const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
  1988. const uint8_t *end,
  1989. uint32_t * restrict state)
  1990. {
  1991. int i;
  1992. assert(p <= end);
  1993. if (p >= end)
  1994. return end;
  1995. for (i = 0; i < 3; i++) {
  1996. uint32_t tmp = *state << 8;
  1997. *state = tmp + *(p++);
  1998. if (tmp == 0x100 || p == end)
  1999. return p;
  2000. }
  2001. while (p < end) {
  2002. if (p[-1] > 1 ) p += 3;
  2003. else if (p[-2] ) p += 2;
  2004. else if (p[-3]|(p[-1]-1)) p++;
  2005. else {
  2006. p++;
  2007. break;
  2008. }
  2009. }
  2010. p = FFMIN(p, end) - 4;
  2011. *state = AV_RB32(p);
  2012. return p + 4;
  2013. }