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.

2323 lines
69KB

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