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