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.

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