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.

2294 lines
68KB

  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. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  55. {
  56. if (min_size < *size)
  57. return ptr;
  58. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  59. ptr = av_realloc(ptr, min_size);
  60. /* we could set this to the unmodified min_size but this is safer
  61. * if the user lost the ptr and uses NULL now
  62. */
  63. if (!ptr)
  64. min_size = 0;
  65. *size = min_size;
  66. return ptr;
  67. }
  68. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  69. {
  70. void **p = ptr;
  71. if (min_size < *size)
  72. return;
  73. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  74. av_free(*p);
  75. *p = av_malloc(min_size);
  76. if (!*p)
  77. min_size = 0;
  78. *size = min_size;
  79. }
  80. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  81. {
  82. void **p = ptr;
  83. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  84. av_freep(p);
  85. *size = 0;
  86. return;
  87. }
  88. av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  89. if (*size)
  90. memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  91. }
  92. /* encoder management */
  93. static AVCodec *first_avcodec = NULL;
  94. AVCodec *av_codec_next(const AVCodec *c)
  95. {
  96. if (c)
  97. return c->next;
  98. else
  99. return first_avcodec;
  100. }
  101. static av_cold void avcodec_init(void)
  102. {
  103. static int initialized = 0;
  104. if (initialized != 0)
  105. return;
  106. initialized = 1;
  107. if (CONFIG_DSPUTIL)
  108. ff_dsputil_static_init();
  109. }
  110. int av_codec_is_encoder(const AVCodec *codec)
  111. {
  112. return codec && (codec->encode_sub || codec->encode2);
  113. }
  114. int av_codec_is_decoder(const AVCodec *codec)
  115. {
  116. return codec && codec->decode;
  117. }
  118. av_cold void avcodec_register(AVCodec *codec)
  119. {
  120. AVCodec **p;
  121. avcodec_init();
  122. p = &first_avcodec;
  123. while (*p != NULL)
  124. p = &(*p)->next;
  125. *p = codec;
  126. codec->next = NULL;
  127. if (codec->init_static_data)
  128. codec->init_static_data(codec);
  129. }
  130. unsigned avcodec_get_edge_width(void)
  131. {
  132. return EDGE_WIDTH;
  133. }
  134. #if FF_API_SET_DIMENSIONS
  135. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  136. {
  137. ff_set_dimensions(s, width, height);
  138. }
  139. #endif
  140. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  141. {
  142. int ret = av_image_check_size(width, height, 0, s);
  143. if (ret < 0)
  144. width = height = 0;
  145. s->width = s->coded_width = width;
  146. s->height = s->coded_height = height;
  147. return ret;
  148. }
  149. #if HAVE_NEON || ARCH_PPC || HAVE_MMX
  150. # define STRIDE_ALIGN 16
  151. #else
  152. # define STRIDE_ALIGN 8
  153. #endif
  154. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  155. int linesize_align[AV_NUM_DATA_POINTERS])
  156. {
  157. int i;
  158. int w_align = 1;
  159. int h_align = 1;
  160. switch (s->pix_fmt) {
  161. case AV_PIX_FMT_YUV420P:
  162. case AV_PIX_FMT_YUYV422:
  163. case AV_PIX_FMT_UYVY422:
  164. case AV_PIX_FMT_YUV422P:
  165. case AV_PIX_FMT_YUV440P:
  166. case AV_PIX_FMT_YUV444P:
  167. case AV_PIX_FMT_GBRP:
  168. case AV_PIX_FMT_GRAY8:
  169. case AV_PIX_FMT_GRAY16BE:
  170. case AV_PIX_FMT_GRAY16LE:
  171. case AV_PIX_FMT_YUVJ420P:
  172. case AV_PIX_FMT_YUVJ422P:
  173. case AV_PIX_FMT_YUVJ440P:
  174. case AV_PIX_FMT_YUVJ444P:
  175. case AV_PIX_FMT_YUVA420P:
  176. case AV_PIX_FMT_YUVA422P:
  177. case AV_PIX_FMT_YUVA444P:
  178. case AV_PIX_FMT_YUV420P9LE:
  179. case AV_PIX_FMT_YUV420P9BE:
  180. case AV_PIX_FMT_YUV420P10LE:
  181. case AV_PIX_FMT_YUV420P10BE:
  182. case AV_PIX_FMT_YUV422P9LE:
  183. case AV_PIX_FMT_YUV422P9BE:
  184. case AV_PIX_FMT_YUV422P10LE:
  185. case AV_PIX_FMT_YUV422P10BE:
  186. case AV_PIX_FMT_YUV444P9LE:
  187. case AV_PIX_FMT_YUV444P9BE:
  188. case AV_PIX_FMT_YUV444P10LE:
  189. case AV_PIX_FMT_YUV444P10BE:
  190. case AV_PIX_FMT_GBRP9LE:
  191. case AV_PIX_FMT_GBRP9BE:
  192. case AV_PIX_FMT_GBRP10LE:
  193. case AV_PIX_FMT_GBRP10BE:
  194. w_align = 16; //FIXME assume 16 pixel per macroblock
  195. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  196. break;
  197. case AV_PIX_FMT_YUV411P:
  198. case AV_PIX_FMT_UYYVYY411:
  199. w_align = 32;
  200. h_align = 8;
  201. break;
  202. case AV_PIX_FMT_YUV410P:
  203. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  204. w_align = 64;
  205. h_align = 64;
  206. }
  207. case AV_PIX_FMT_RGB555:
  208. if (s->codec_id == AV_CODEC_ID_RPZA) {
  209. w_align = 4;
  210. h_align = 4;
  211. }
  212. case AV_PIX_FMT_PAL8:
  213. case AV_PIX_FMT_BGR8:
  214. case AV_PIX_FMT_RGB8:
  215. if (s->codec_id == AV_CODEC_ID_SMC) {
  216. w_align = 4;
  217. h_align = 4;
  218. }
  219. break;
  220. case AV_PIX_FMT_BGR24:
  221. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  222. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  223. w_align = 4;
  224. h_align = 4;
  225. }
  226. break;
  227. default:
  228. w_align = 1;
  229. h_align = 1;
  230. break;
  231. }
  232. *width = FFALIGN(*width, w_align);
  233. *height = FFALIGN(*height, h_align);
  234. if (s->codec_id == AV_CODEC_ID_H264)
  235. // some of the optimized chroma MC reads one line too much
  236. *height += 2;
  237. for (i = 0; i < 4; i++)
  238. linesize_align[i] = STRIDE_ALIGN;
  239. }
  240. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  241. {
  242. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  243. int chroma_shift = desc->log2_chroma_w;
  244. int linesize_align[AV_NUM_DATA_POINTERS];
  245. int align;
  246. avcodec_align_dimensions2(s, width, height, linesize_align);
  247. align = FFMAX(linesize_align[0], linesize_align[3]);
  248. linesize_align[1] <<= chroma_shift;
  249. linesize_align[2] <<= chroma_shift;
  250. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  251. *width = FFALIGN(*width, align);
  252. }
  253. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  254. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  255. int buf_size, int align)
  256. {
  257. int ch, planar, needed_size, ret = 0;
  258. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  259. frame->nb_samples, sample_fmt,
  260. align);
  261. if (buf_size < needed_size)
  262. return AVERROR(EINVAL);
  263. planar = av_sample_fmt_is_planar(sample_fmt);
  264. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  265. if (!(frame->extended_data = av_mallocz(nb_channels *
  266. sizeof(*frame->extended_data))))
  267. return AVERROR(ENOMEM);
  268. } else {
  269. frame->extended_data = frame->data;
  270. }
  271. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  272. buf, nb_channels, frame->nb_samples,
  273. sample_fmt, align)) < 0) {
  274. if (frame->extended_data != frame->data)
  275. av_free(frame->extended_data);
  276. return ret;
  277. }
  278. if (frame->extended_data != frame->data) {
  279. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  280. frame->data[ch] = frame->extended_data[ch];
  281. }
  282. return ret;
  283. }
  284. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  285. {
  286. FramePool *pool = avctx->internal->pool;
  287. int i, ret;
  288. switch (avctx->codec_type) {
  289. case AVMEDIA_TYPE_VIDEO: {
  290. AVPicture picture;
  291. int size[4] = { 0 };
  292. int w = frame->width;
  293. int h = frame->height;
  294. int tmpsize, unaligned;
  295. if (pool->format == frame->format &&
  296. pool->width == frame->width && pool->height == frame->height)
  297. return 0;
  298. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  299. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  300. w += EDGE_WIDTH * 2;
  301. h += EDGE_WIDTH * 2;
  302. }
  303. do {
  304. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  305. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  306. av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
  307. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  308. w += w & ~(w - 1);
  309. unaligned = 0;
  310. for (i = 0; i < 4; i++)
  311. unaligned |= picture.linesize[i] % pool->stride_align[i];
  312. } while (unaligned);
  313. tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
  314. NULL, picture.linesize);
  315. if (tmpsize < 0)
  316. return -1;
  317. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  318. size[i] = picture.data[i + 1] - picture.data[i];
  319. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  320. for (i = 0; i < 4; i++) {
  321. av_buffer_pool_uninit(&pool->pools[i]);
  322. pool->linesize[i] = picture.linesize[i];
  323. if (size[i]) {
  324. pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
  325. if (!pool->pools[i]) {
  326. ret = AVERROR(ENOMEM);
  327. goto fail;
  328. }
  329. }
  330. }
  331. pool->format = frame->format;
  332. pool->width = frame->width;
  333. pool->height = frame->height;
  334. break;
  335. }
  336. case AVMEDIA_TYPE_AUDIO: {
  337. int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
  338. int planar = av_sample_fmt_is_planar(frame->format);
  339. int planes = planar ? ch : 1;
  340. if (pool->format == frame->format && pool->planes == planes &&
  341. pool->channels == ch && frame->nb_samples == pool->samples)
  342. return 0;
  343. av_buffer_pool_uninit(&pool->pools[0]);
  344. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  345. frame->nb_samples, frame->format, 0);
  346. if (ret < 0)
  347. goto fail;
  348. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  349. if (!pool->pools[0]) {
  350. ret = AVERROR(ENOMEM);
  351. goto fail;
  352. }
  353. pool->format = frame->format;
  354. pool->planes = planes;
  355. pool->channels = ch;
  356. pool->samples = frame->nb_samples;
  357. break;
  358. }
  359. default: av_assert0(0);
  360. }
  361. return 0;
  362. fail:
  363. for (i = 0; i < 4; i++)
  364. av_buffer_pool_uninit(&pool->pools[i]);
  365. pool->format = -1;
  366. pool->planes = pool->channels = pool->samples = 0;
  367. pool->width = pool->height = 0;
  368. return ret;
  369. }
  370. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  371. {
  372. FramePool *pool = avctx->internal->pool;
  373. int planes = pool->planes;
  374. int i;
  375. frame->linesize[0] = pool->linesize[0];
  376. if (planes > AV_NUM_DATA_POINTERS) {
  377. frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  378. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  379. frame->extended_buf = av_mallocz(frame->nb_extended_buf *
  380. sizeof(*frame->extended_buf));
  381. if (!frame->extended_data || !frame->extended_buf) {
  382. av_freep(&frame->extended_data);
  383. av_freep(&frame->extended_buf);
  384. return AVERROR(ENOMEM);
  385. }
  386. } else
  387. frame->extended_data = frame->data;
  388. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  389. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  390. if (!frame->buf[i])
  391. goto fail;
  392. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  393. }
  394. for (i = 0; i < frame->nb_extended_buf; i++) {
  395. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  396. if (!frame->extended_buf[i])
  397. goto fail;
  398. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  399. }
  400. if (avctx->debug & FF_DEBUG_BUFFERS)
  401. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  402. return 0;
  403. fail:
  404. av_frame_unref(frame);
  405. return AVERROR(ENOMEM);
  406. }
  407. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  408. {
  409. FramePool *pool = s->internal->pool;
  410. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
  411. int pixel_size = desc->comp[0].step_minus1 + 1;
  412. int h_chroma_shift, v_chroma_shift;
  413. int i;
  414. if (pic->data[0] != NULL) {
  415. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  416. return -1;
  417. }
  418. memset(pic->data, 0, sizeof(pic->data));
  419. pic->extended_data = pic->data;
  420. av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  421. for (i = 0; i < 4 && pool->pools[i]; i++) {
  422. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  423. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  424. pic->linesize[i] = pool->linesize[i];
  425. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  426. if (!pic->buf[i])
  427. goto fail;
  428. // no edge if EDGE EMU or not planar YUV
  429. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
  430. pic->data[i] = pic->buf[i]->data;
  431. else {
  432. pic->data[i] = pic->buf[i]->data +
  433. FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
  434. (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
  435. }
  436. }
  437. for (; i < AV_NUM_DATA_POINTERS; i++) {
  438. pic->data[i] = NULL;
  439. pic->linesize[i] = 0;
  440. }
  441. if (pic->data[1] && !pic->data[2])
  442. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  443. if (s->debug & FF_DEBUG_BUFFERS)
  444. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  445. return 0;
  446. fail:
  447. av_frame_unref(pic);
  448. return AVERROR(ENOMEM);
  449. }
  450. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  451. {
  452. int ret;
  453. if ((ret = update_frame_pool(avctx, frame)) < 0)
  454. return ret;
  455. #if FF_API_GET_BUFFER
  456. FF_DISABLE_DEPRECATION_WARNINGS
  457. frame->type = FF_BUFFER_TYPE_INTERNAL;
  458. FF_ENABLE_DEPRECATION_WARNINGS
  459. #endif
  460. switch (avctx->codec_type) {
  461. case AVMEDIA_TYPE_VIDEO:
  462. return video_get_buffer(avctx, frame);
  463. case AVMEDIA_TYPE_AUDIO:
  464. return audio_get_buffer(avctx, frame);
  465. default:
  466. return -1;
  467. }
  468. }
  469. #if FF_API_GET_BUFFER
  470. FF_DISABLE_DEPRECATION_WARNINGS
  471. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  472. {
  473. return avcodec_default_get_buffer2(avctx, frame, 0);
  474. }
  475. typedef struct CompatReleaseBufPriv {
  476. AVCodecContext avctx;
  477. AVFrame frame;
  478. } CompatReleaseBufPriv;
  479. static void compat_free_buffer(void *opaque, uint8_t *data)
  480. {
  481. CompatReleaseBufPriv *priv = opaque;
  482. if (priv->avctx.release_buffer)
  483. priv->avctx.release_buffer(&priv->avctx, &priv->frame);
  484. av_freep(&priv);
  485. }
  486. static void compat_release_buffer(void *opaque, uint8_t *data)
  487. {
  488. AVBufferRef *buf = opaque;
  489. av_buffer_unref(&buf);
  490. }
  491. FF_ENABLE_DEPRECATION_WARNINGS
  492. #endif
  493. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  494. {
  495. int ret;
  496. switch (avctx->codec_type) {
  497. case AVMEDIA_TYPE_VIDEO:
  498. frame->width = FFMAX(avctx->width, avctx->coded_width);
  499. frame->height = FFMAX(avctx->height, avctx->coded_height);
  500. if (frame->format < 0)
  501. frame->format = avctx->pix_fmt;
  502. if (!frame->sample_aspect_ratio.num)
  503. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  504. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  505. return ret;
  506. break;
  507. case AVMEDIA_TYPE_AUDIO:
  508. if (!frame->sample_rate)
  509. frame->sample_rate = avctx->sample_rate;
  510. if (frame->format < 0)
  511. frame->format = avctx->sample_fmt;
  512. if (!frame->channel_layout) {
  513. if (avctx->channel_layout) {
  514. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  515. avctx->channels) {
  516. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  517. "configuration.\n");
  518. return AVERROR(EINVAL);
  519. }
  520. frame->channel_layout = avctx->channel_layout;
  521. } else {
  522. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  523. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  524. avctx->channels);
  525. return AVERROR(ENOSYS);
  526. }
  527. frame->channel_layout = av_get_default_channel_layout(avctx->channels);
  528. if (!frame->channel_layout)
  529. frame->channel_layout = (1ULL << avctx->channels) - 1;
  530. }
  531. }
  532. break;
  533. default: return AVERROR(EINVAL);
  534. }
  535. frame->pkt_pts = avctx->pkt ? avctx->pkt->pts : AV_NOPTS_VALUE;
  536. frame->reordered_opaque = avctx->reordered_opaque;
  537. #if FF_API_GET_BUFFER
  538. FF_DISABLE_DEPRECATION_WARNINGS
  539. /*
  540. * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
  541. * We wrap each plane in its own AVBuffer. Each of those has a reference to
  542. * a dummy AVBuffer as its private data, unreffing it on free.
  543. * When all the planes are freed, the dummy buffer's free callback calls
  544. * release_buffer().
  545. */
  546. if (avctx->get_buffer) {
  547. CompatReleaseBufPriv *priv = NULL;
  548. AVBufferRef *dummy_buf = NULL;
  549. int planes, i, ret;
  550. if (flags & AV_GET_BUFFER_FLAG_REF)
  551. frame->reference = 1;
  552. ret = avctx->get_buffer(avctx, frame);
  553. if (ret < 0)
  554. return ret;
  555. /* return if the buffers are already set up
  556. * this would happen e.g. when a custom get_buffer() calls
  557. * avcodec_default_get_buffer
  558. */
  559. if (frame->buf[0])
  560. return 0;
  561. priv = av_mallocz(sizeof(*priv));
  562. if (!priv) {
  563. ret = AVERROR(ENOMEM);
  564. goto fail;
  565. }
  566. priv->avctx = *avctx;
  567. priv->frame = *frame;
  568. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
  569. if (!dummy_buf) {
  570. ret = AVERROR(ENOMEM);
  571. goto fail;
  572. }
  573. #define WRAP_PLANE(ref_out, data, data_size) \
  574. do { \
  575. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  576. if (!dummy_ref) { \
  577. ret = AVERROR(ENOMEM); \
  578. goto fail; \
  579. } \
  580. ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
  581. dummy_ref, 0); \
  582. if (!ref_out) { \
  583. av_frame_unref(frame); \
  584. ret = AVERROR(ENOMEM); \
  585. goto fail; \
  586. } \
  587. } while (0)
  588. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  589. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  590. planes = av_pix_fmt_count_planes(frame->format);
  591. /* workaround for AVHWAccel plane count of 0, buf[0] is used as
  592. check for allocated buffers: make libavcodec happy */
  593. if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  594. planes = 1;
  595. if (!desc || planes <= 0) {
  596. ret = AVERROR(EINVAL);
  597. goto fail;
  598. }
  599. for (i = 0; i < planes; i++) {
  600. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  601. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  602. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  603. }
  604. } else {
  605. int planar = av_sample_fmt_is_planar(frame->format);
  606. planes = planar ? avctx->channels : 1;
  607. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  608. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  609. frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
  610. frame->nb_extended_buf);
  611. if (!frame->extended_buf) {
  612. ret = AVERROR(ENOMEM);
  613. goto fail;
  614. }
  615. }
  616. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  617. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  618. for (i = 0; i < frame->nb_extended_buf; i++)
  619. WRAP_PLANE(frame->extended_buf[i],
  620. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  621. frame->linesize[0]);
  622. }
  623. av_buffer_unref(&dummy_buf);
  624. frame->width = avctx->width;
  625. frame->height = avctx->height;
  626. return 0;
  627. fail:
  628. avctx->release_buffer(avctx, frame);
  629. av_freep(&priv);
  630. av_buffer_unref(&dummy_buf);
  631. return ret;
  632. }
  633. FF_ENABLE_DEPRECATION_WARNINGS
  634. #endif
  635. ret = avctx->get_buffer2(avctx, frame, flags);
  636. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  637. frame->width = avctx->width;
  638. frame->height = avctx->height;
  639. }
  640. return ret;
  641. }
  642. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  643. {
  644. AVFrame tmp;
  645. int ret;
  646. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  647. if (!frame->data[0])
  648. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  649. if (av_frame_is_writable(frame))
  650. return 0;
  651. av_frame_move_ref(&tmp, frame);
  652. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  653. if (ret < 0) {
  654. av_frame_unref(&tmp);
  655. return ret;
  656. }
  657. av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
  658. frame->format, frame->width, frame->height);
  659. av_frame_unref(&tmp);
  660. return 0;
  661. }
  662. #if FF_API_GET_BUFFER
  663. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  664. {
  665. av_frame_unref(pic);
  666. }
  667. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  668. {
  669. av_assert0(0);
  670. return AVERROR_BUG;
  671. }
  672. #endif
  673. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  674. {
  675. int i;
  676. for (i = 0; i < count; i++) {
  677. int r = func(c, (char *)arg + i * size);
  678. if (ret)
  679. ret[i] = r;
  680. }
  681. return 0;
  682. }
  683. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  684. {
  685. int i;
  686. for (i = 0; i < count; i++) {
  687. int r = func(c, arg, i, 0);
  688. if (ret)
  689. ret[i] = r;
  690. }
  691. return 0;
  692. }
  693. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  694. {
  695. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  696. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  697. }
  698. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  699. {
  700. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  701. ++fmt;
  702. return fmt[0];
  703. }
  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. AVFrame *f;
  726. if (!frame || !*frame)
  727. return;
  728. f = *frame;
  729. if (f->extended_data != f->data)
  730. av_freep(&f->extended_data);
  731. av_freep(frame);
  732. }
  733. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  734. {
  735. int ret = 0;
  736. AVDictionary *tmp = NULL;
  737. if (avcodec_is_open(avctx))
  738. return 0;
  739. if ((!codec && !avctx->codec)) {
  740. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  741. return AVERROR(EINVAL);
  742. }
  743. if ((codec && avctx->codec && codec != avctx->codec)) {
  744. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  745. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  746. return AVERROR(EINVAL);
  747. }
  748. if (!codec)
  749. codec = avctx->codec;
  750. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  751. return AVERROR(EINVAL);
  752. if (options)
  753. av_dict_copy(&tmp, *options, 0);
  754. /* If there is a user-supplied mutex locking routine, call it. */
  755. if (lockmgr_cb) {
  756. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  757. return -1;
  758. }
  759. entangled_thread_counter++;
  760. if (entangled_thread_counter != 1) {
  761. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  762. ret = -1;
  763. goto end;
  764. }
  765. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  766. if (!avctx->internal) {
  767. ret = AVERROR(ENOMEM);
  768. goto end;
  769. }
  770. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  771. if (!avctx->internal->pool) {
  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 = avcodec_alloc_frame()))
  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. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1194. int *got_picture_ptr,
  1195. AVPacket *avpkt)
  1196. {
  1197. AVCodecInternal *avci = avctx->internal;
  1198. int ret;
  1199. *got_picture_ptr = 0;
  1200. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1201. return -1;
  1202. avctx->pkt = avpkt;
  1203. ret = apply_param_change(avctx, avpkt);
  1204. if (ret < 0) {
  1205. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1206. if (avctx->err_recognition & AV_EF_EXPLODE)
  1207. return ret;
  1208. }
  1209. avcodec_get_frame_defaults(picture);
  1210. if (!avctx->refcounted_frames)
  1211. av_frame_unref(&avci->to_free);
  1212. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1213. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1214. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1215. avpkt);
  1216. else {
  1217. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1218. avpkt);
  1219. picture->pkt_dts = avpkt->dts;
  1220. /* get_buffer is supposed to set frame parameters */
  1221. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1222. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1223. picture->width = avctx->width;
  1224. picture->height = avctx->height;
  1225. picture->format = avctx->pix_fmt;
  1226. }
  1227. }
  1228. emms_c(); //needed to avoid an emms_c() call before every return;
  1229. if (ret < 0 && picture->data[0])
  1230. av_frame_unref(picture);
  1231. if (*got_picture_ptr) {
  1232. if (!avctx->refcounted_frames) {
  1233. avci->to_free = *picture;
  1234. avci->to_free.extended_data = avci->to_free.data;
  1235. memset(picture->buf, 0, sizeof(picture->buf));
  1236. }
  1237. avctx->frame_number++;
  1238. }
  1239. } else
  1240. ret = 0;
  1241. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1242. * make sure it's set correctly */
  1243. picture->extended_data = picture->data;
  1244. return ret;
  1245. }
  1246. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1247. AVFrame *frame,
  1248. int *got_frame_ptr,
  1249. AVPacket *avpkt)
  1250. {
  1251. AVCodecInternal *avci = avctx->internal;
  1252. int planar, channels;
  1253. int ret = 0;
  1254. *got_frame_ptr = 0;
  1255. avctx->pkt = avpkt;
  1256. if (!avpkt->data && avpkt->size) {
  1257. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1258. return AVERROR(EINVAL);
  1259. }
  1260. ret = apply_param_change(avctx, avpkt);
  1261. if (ret < 0) {
  1262. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1263. if (avctx->err_recognition & AV_EF_EXPLODE)
  1264. return ret;
  1265. }
  1266. avcodec_get_frame_defaults(frame);
  1267. if (!avctx->refcounted_frames)
  1268. av_frame_unref(&avci->to_free);
  1269. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1270. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1271. if (ret >= 0 && *got_frame_ptr) {
  1272. avctx->frame_number++;
  1273. frame->pkt_dts = avpkt->dts;
  1274. if (frame->format == AV_SAMPLE_FMT_NONE)
  1275. frame->format = avctx->sample_fmt;
  1276. if (!avctx->refcounted_frames) {
  1277. avci->to_free = *frame;
  1278. avci->to_free.extended_data = avci->to_free.data;
  1279. memset(frame->buf, 0, sizeof(frame->buf));
  1280. frame->extended_buf = NULL;
  1281. frame->nb_extended_buf = 0;
  1282. }
  1283. } else if (frame->data[0])
  1284. av_frame_unref(frame);
  1285. }
  1286. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1287. * make sure it's set correctly; assume decoders that actually use
  1288. * extended_data are doing it correctly */
  1289. planar = av_sample_fmt_is_planar(frame->format);
  1290. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1291. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1292. frame->extended_data = frame->data;
  1293. return ret;
  1294. }
  1295. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1296. int *got_sub_ptr,
  1297. AVPacket *avpkt)
  1298. {
  1299. int ret;
  1300. avctx->pkt = avpkt;
  1301. *got_sub_ptr = 0;
  1302. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1303. if (*got_sub_ptr)
  1304. avctx->frame_number++;
  1305. return ret;
  1306. }
  1307. void avsubtitle_free(AVSubtitle *sub)
  1308. {
  1309. int i;
  1310. for (i = 0; i < sub->num_rects; i++) {
  1311. av_freep(&sub->rects[i]->pict.data[0]);
  1312. av_freep(&sub->rects[i]->pict.data[1]);
  1313. av_freep(&sub->rects[i]->pict.data[2]);
  1314. av_freep(&sub->rects[i]->pict.data[3]);
  1315. av_freep(&sub->rects[i]->text);
  1316. av_freep(&sub->rects[i]->ass);
  1317. av_freep(&sub->rects[i]);
  1318. }
  1319. av_freep(&sub->rects);
  1320. memset(sub, 0, sizeof(AVSubtitle));
  1321. }
  1322. av_cold int avcodec_close(AVCodecContext *avctx)
  1323. {
  1324. /* If there is a user-supplied mutex locking routine, call it. */
  1325. if (lockmgr_cb) {
  1326. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1327. return -1;
  1328. }
  1329. entangled_thread_counter++;
  1330. if (entangled_thread_counter != 1) {
  1331. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1332. entangled_thread_counter--;
  1333. return -1;
  1334. }
  1335. if (avcodec_is_open(avctx)) {
  1336. FramePool *pool = avctx->internal->pool;
  1337. int i;
  1338. if (HAVE_THREADS && avctx->thread_opaque)
  1339. ff_thread_free(avctx);
  1340. if (avctx->codec && avctx->codec->close)
  1341. avctx->codec->close(avctx);
  1342. avctx->coded_frame = NULL;
  1343. if (!avctx->refcounted_frames)
  1344. av_frame_unref(&avctx->internal->to_free);
  1345. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  1346. av_buffer_pool_uninit(&pool->pools[i]);
  1347. av_freep(&avctx->internal->pool);
  1348. av_freep(&avctx->internal);
  1349. }
  1350. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1351. av_opt_free(avctx->priv_data);
  1352. av_opt_free(avctx);
  1353. av_freep(&avctx->priv_data);
  1354. if (av_codec_is_encoder(avctx->codec))
  1355. av_freep(&avctx->extradata);
  1356. avctx->codec = NULL;
  1357. avctx->active_thread_type = 0;
  1358. entangled_thread_counter--;
  1359. /* Release any user-supplied mutex. */
  1360. if (lockmgr_cb) {
  1361. (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1362. }
  1363. return 0;
  1364. }
  1365. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1366. {
  1367. AVCodec *p, *experimental = NULL;
  1368. p = first_avcodec;
  1369. while (p) {
  1370. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1371. p->id == id) {
  1372. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1373. experimental = p;
  1374. } else
  1375. return p;
  1376. }
  1377. p = p->next;
  1378. }
  1379. return experimental;
  1380. }
  1381. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1382. {
  1383. return find_encdec(id, 1);
  1384. }
  1385. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1386. {
  1387. AVCodec *p;
  1388. if (!name)
  1389. return NULL;
  1390. p = first_avcodec;
  1391. while (p) {
  1392. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1393. return p;
  1394. p = p->next;
  1395. }
  1396. return NULL;
  1397. }
  1398. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1399. {
  1400. return find_encdec(id, 0);
  1401. }
  1402. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1403. {
  1404. AVCodec *p;
  1405. if (!name)
  1406. return NULL;
  1407. p = first_avcodec;
  1408. while (p) {
  1409. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1410. return p;
  1411. p = p->next;
  1412. }
  1413. return NULL;
  1414. }
  1415. static int get_bit_rate(AVCodecContext *ctx)
  1416. {
  1417. int bit_rate;
  1418. int bits_per_sample;
  1419. switch (ctx->codec_type) {
  1420. case AVMEDIA_TYPE_VIDEO:
  1421. case AVMEDIA_TYPE_DATA:
  1422. case AVMEDIA_TYPE_SUBTITLE:
  1423. case AVMEDIA_TYPE_ATTACHMENT:
  1424. bit_rate = ctx->bit_rate;
  1425. break;
  1426. case AVMEDIA_TYPE_AUDIO:
  1427. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1428. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1429. break;
  1430. default:
  1431. bit_rate = 0;
  1432. break;
  1433. }
  1434. return bit_rate;
  1435. }
  1436. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1437. {
  1438. int i, len, ret = 0;
  1439. #define TAG_PRINT(x) \
  1440. (((x) >= '0' && (x) <= '9') || \
  1441. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1442. ((x) == '.' || (x) == ' '))
  1443. for (i = 0; i < 4; i++) {
  1444. len = snprintf(buf, buf_size,
  1445. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1446. buf += len;
  1447. buf_size = buf_size > len ? buf_size - len : 0;
  1448. ret += len;
  1449. codec_tag >>= 8;
  1450. }
  1451. return ret;
  1452. }
  1453. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1454. {
  1455. const char *codec_name;
  1456. const char *profile = NULL;
  1457. const AVCodec *p;
  1458. char buf1[32];
  1459. int bitrate;
  1460. AVRational display_aspect_ratio;
  1461. if (enc->codec)
  1462. p = enc->codec;
  1463. else if (encode)
  1464. p = avcodec_find_encoder(enc->codec_id);
  1465. else
  1466. p = avcodec_find_decoder(enc->codec_id);
  1467. if (p) {
  1468. codec_name = p->name;
  1469. profile = av_get_profile_name(p, enc->profile);
  1470. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1471. /* fake mpeg2 transport stream codec (currently not
  1472. * registered) */
  1473. codec_name = "mpeg2ts";
  1474. } else if (enc->codec_name[0] != '\0') {
  1475. codec_name = enc->codec_name;
  1476. } else {
  1477. /* output avi tags */
  1478. char tag_buf[32];
  1479. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1480. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1481. codec_name = buf1;
  1482. }
  1483. switch (enc->codec_type) {
  1484. case AVMEDIA_TYPE_VIDEO:
  1485. snprintf(buf, buf_size,
  1486. "Video: %s%s",
  1487. codec_name, enc->mb_decision ? " (hq)" : "");
  1488. if (profile)
  1489. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1490. " (%s)", profile);
  1491. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1492. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1493. ", %s",
  1494. av_get_pix_fmt_name(enc->pix_fmt));
  1495. }
  1496. if (enc->width) {
  1497. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1498. ", %dx%d",
  1499. enc->width, enc->height);
  1500. if (enc->sample_aspect_ratio.num) {
  1501. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1502. enc->width * enc->sample_aspect_ratio.num,
  1503. enc->height * enc->sample_aspect_ratio.den,
  1504. 1024 * 1024);
  1505. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1506. " [PAR %d:%d DAR %d:%d]",
  1507. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1508. display_aspect_ratio.num, display_aspect_ratio.den);
  1509. }
  1510. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1511. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1512. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1513. ", %d/%d",
  1514. enc->time_base.num / g, enc->time_base.den / g);
  1515. }
  1516. }
  1517. if (encode) {
  1518. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1519. ", q=%d-%d", enc->qmin, enc->qmax);
  1520. }
  1521. break;
  1522. case AVMEDIA_TYPE_AUDIO:
  1523. snprintf(buf, buf_size,
  1524. "Audio: %s",
  1525. codec_name);
  1526. if (profile)
  1527. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1528. " (%s)", profile);
  1529. if (enc->sample_rate) {
  1530. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1531. ", %d Hz", enc->sample_rate);
  1532. }
  1533. av_strlcat(buf, ", ", buf_size);
  1534. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1535. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1536. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1537. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1538. }
  1539. break;
  1540. case AVMEDIA_TYPE_DATA:
  1541. snprintf(buf, buf_size, "Data: %s", codec_name);
  1542. break;
  1543. case AVMEDIA_TYPE_SUBTITLE:
  1544. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1545. break;
  1546. case AVMEDIA_TYPE_ATTACHMENT:
  1547. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1548. break;
  1549. default:
  1550. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1551. return;
  1552. }
  1553. if (encode) {
  1554. if (enc->flags & CODEC_FLAG_PASS1)
  1555. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1556. ", pass 1");
  1557. if (enc->flags & CODEC_FLAG_PASS2)
  1558. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1559. ", pass 2");
  1560. }
  1561. bitrate = get_bit_rate(enc);
  1562. if (bitrate != 0) {
  1563. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1564. ", %d kb/s", bitrate / 1000);
  1565. }
  1566. }
  1567. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1568. {
  1569. const AVProfile *p;
  1570. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1571. return NULL;
  1572. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1573. if (p->profile == profile)
  1574. return p->name;
  1575. return NULL;
  1576. }
  1577. unsigned avcodec_version(void)
  1578. {
  1579. return LIBAVCODEC_VERSION_INT;
  1580. }
  1581. const char *avcodec_configuration(void)
  1582. {
  1583. return LIBAV_CONFIGURATION;
  1584. }
  1585. const char *avcodec_license(void)
  1586. {
  1587. #define LICENSE_PREFIX "libavcodec license: "
  1588. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1589. }
  1590. void avcodec_flush_buffers(AVCodecContext *avctx)
  1591. {
  1592. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1593. ff_thread_flush(avctx);
  1594. else if (avctx->codec->flush)
  1595. avctx->codec->flush(avctx);
  1596. if (!avctx->refcounted_frames)
  1597. av_frame_unref(&avctx->internal->to_free);
  1598. }
  1599. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1600. {
  1601. switch (codec_id) {
  1602. case AV_CODEC_ID_ADPCM_CT:
  1603. case AV_CODEC_ID_ADPCM_IMA_APC:
  1604. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1605. case AV_CODEC_ID_ADPCM_IMA_WS:
  1606. case AV_CODEC_ID_ADPCM_G722:
  1607. case AV_CODEC_ID_ADPCM_YAMAHA:
  1608. return 4;
  1609. case AV_CODEC_ID_PCM_ALAW:
  1610. case AV_CODEC_ID_PCM_MULAW:
  1611. case AV_CODEC_ID_PCM_S8:
  1612. case AV_CODEC_ID_PCM_U8:
  1613. case AV_CODEC_ID_PCM_ZORK:
  1614. return 8;
  1615. case AV_CODEC_ID_PCM_S16BE:
  1616. case AV_CODEC_ID_PCM_S16LE:
  1617. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1618. case AV_CODEC_ID_PCM_U16BE:
  1619. case AV_CODEC_ID_PCM_U16LE:
  1620. return 16;
  1621. case AV_CODEC_ID_PCM_S24DAUD:
  1622. case AV_CODEC_ID_PCM_S24BE:
  1623. case AV_CODEC_ID_PCM_S24LE:
  1624. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  1625. case AV_CODEC_ID_PCM_U24BE:
  1626. case AV_CODEC_ID_PCM_U24LE:
  1627. return 24;
  1628. case AV_CODEC_ID_PCM_S32BE:
  1629. case AV_CODEC_ID_PCM_S32LE:
  1630. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  1631. case AV_CODEC_ID_PCM_U32BE:
  1632. case AV_CODEC_ID_PCM_U32LE:
  1633. case AV_CODEC_ID_PCM_F32BE:
  1634. case AV_CODEC_ID_PCM_F32LE:
  1635. return 32;
  1636. case AV_CODEC_ID_PCM_F64BE:
  1637. case AV_CODEC_ID_PCM_F64LE:
  1638. return 64;
  1639. default:
  1640. return 0;
  1641. }
  1642. }
  1643. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1644. {
  1645. switch (codec_id) {
  1646. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1647. return 2;
  1648. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1649. return 3;
  1650. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1651. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1652. case AV_CODEC_ID_ADPCM_IMA_QT:
  1653. case AV_CODEC_ID_ADPCM_SWF:
  1654. case AV_CODEC_ID_ADPCM_MS:
  1655. return 4;
  1656. default:
  1657. return av_get_exact_bits_per_sample(codec_id);
  1658. }
  1659. }
  1660. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1661. {
  1662. int id, sr, ch, ba, tag, bps;
  1663. id = avctx->codec_id;
  1664. sr = avctx->sample_rate;
  1665. ch = avctx->channels;
  1666. ba = avctx->block_align;
  1667. tag = avctx->codec_tag;
  1668. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1669. /* codecs with an exact constant bits per sample */
  1670. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1671. return (frame_bytes * 8) / (bps * ch);
  1672. bps = avctx->bits_per_coded_sample;
  1673. /* codecs with a fixed packet duration */
  1674. switch (id) {
  1675. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1676. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1677. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1678. case AV_CODEC_ID_AMR_NB:
  1679. case AV_CODEC_ID_GSM:
  1680. case AV_CODEC_ID_QCELP:
  1681. case AV_CODEC_ID_RA_144:
  1682. case AV_CODEC_ID_RA_288: return 160;
  1683. case AV_CODEC_ID_IMC: return 256;
  1684. case AV_CODEC_ID_AMR_WB:
  1685. case AV_CODEC_ID_GSM_MS: return 320;
  1686. case AV_CODEC_ID_MP1: return 384;
  1687. case AV_CODEC_ID_ATRAC1: return 512;
  1688. case AV_CODEC_ID_ATRAC3: return 1024;
  1689. case AV_CODEC_ID_MP2:
  1690. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1691. case AV_CODEC_ID_AC3: return 1536;
  1692. }
  1693. if (sr > 0) {
  1694. /* calc from sample rate */
  1695. if (id == AV_CODEC_ID_TTA)
  1696. return 256 * sr / 245;
  1697. if (ch > 0) {
  1698. /* calc from sample rate and channels */
  1699. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1700. return (480 << (sr / 22050)) / ch;
  1701. }
  1702. }
  1703. if (ba > 0) {
  1704. /* calc from block_align */
  1705. if (id == AV_CODEC_ID_SIPR) {
  1706. switch (ba) {
  1707. case 20: return 160;
  1708. case 19: return 144;
  1709. case 29: return 288;
  1710. case 37: return 480;
  1711. }
  1712. } else if (id == AV_CODEC_ID_ILBC) {
  1713. switch (ba) {
  1714. case 38: return 160;
  1715. case 50: return 240;
  1716. }
  1717. }
  1718. }
  1719. if (frame_bytes > 0) {
  1720. /* calc from frame_bytes only */
  1721. if (id == AV_CODEC_ID_TRUESPEECH)
  1722. return 240 * (frame_bytes / 32);
  1723. if (id == AV_CODEC_ID_NELLYMOSER)
  1724. return 256 * (frame_bytes / 64);
  1725. if (bps > 0) {
  1726. /* calc from frame_bytes and bits_per_coded_sample */
  1727. if (id == AV_CODEC_ID_ADPCM_G726)
  1728. return frame_bytes * 8 / bps;
  1729. }
  1730. if (ch > 0) {
  1731. /* calc from frame_bytes and channels */
  1732. switch (id) {
  1733. case AV_CODEC_ID_ADPCM_4XM:
  1734. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1735. return (frame_bytes - 4 * ch) * 2 / ch;
  1736. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1737. return (frame_bytes - 4) * 2 / ch;
  1738. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1739. return (frame_bytes - 8) * 2 / ch;
  1740. case AV_CODEC_ID_ADPCM_XA:
  1741. return (frame_bytes / 128) * 224 / ch;
  1742. case AV_CODEC_ID_INTERPLAY_DPCM:
  1743. return (frame_bytes - 6 - ch) / ch;
  1744. case AV_CODEC_ID_ROQ_DPCM:
  1745. return (frame_bytes - 8) / ch;
  1746. case AV_CODEC_ID_XAN_DPCM:
  1747. return (frame_bytes - 2 * ch) / ch;
  1748. case AV_CODEC_ID_MACE3:
  1749. return 3 * frame_bytes / ch;
  1750. case AV_CODEC_ID_MACE6:
  1751. return 6 * frame_bytes / ch;
  1752. case AV_CODEC_ID_PCM_LXF:
  1753. return 2 * (frame_bytes / (5 * ch));
  1754. }
  1755. if (tag) {
  1756. /* calc from frame_bytes, channels, and codec_tag */
  1757. if (id == AV_CODEC_ID_SOL_DPCM) {
  1758. if (tag == 3)
  1759. return frame_bytes / ch;
  1760. else
  1761. return frame_bytes * 2 / ch;
  1762. }
  1763. }
  1764. if (ba > 0) {
  1765. /* calc from frame_bytes, channels, and block_align */
  1766. int blocks = frame_bytes / ba;
  1767. switch (avctx->codec_id) {
  1768. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1769. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1770. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1771. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1772. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1773. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1774. case AV_CODEC_ID_ADPCM_MS:
  1775. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1776. }
  1777. }
  1778. if (bps > 0) {
  1779. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1780. switch (avctx->codec_id) {
  1781. case AV_CODEC_ID_PCM_DVD:
  1782. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1783. case AV_CODEC_ID_PCM_BLURAY:
  1784. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1785. case AV_CODEC_ID_S302M:
  1786. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1787. }
  1788. }
  1789. }
  1790. }
  1791. return 0;
  1792. }
  1793. #if !HAVE_THREADS
  1794. int ff_thread_init(AVCodecContext *s)
  1795. {
  1796. return -1;
  1797. }
  1798. #endif
  1799. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1800. {
  1801. unsigned int n = 0;
  1802. while (v >= 0xff) {
  1803. *s++ = 0xff;
  1804. v -= 0xff;
  1805. n++;
  1806. }
  1807. *s = v;
  1808. n++;
  1809. return n;
  1810. }
  1811. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1812. {
  1813. int i;
  1814. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1815. return i;
  1816. }
  1817. #if FF_API_MISSING_SAMPLE
  1818. FF_DISABLE_DEPRECATION_WARNINGS
  1819. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1820. {
  1821. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
  1822. "version to the newest one from Git. If the problem still "
  1823. "occurs, it means that your file has a feature which has not "
  1824. "been implemented.\n", feature);
  1825. if(want_sample)
  1826. av_log_ask_for_sample(avc, NULL);
  1827. }
  1828. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1829. {
  1830. va_list argument_list;
  1831. va_start(argument_list, msg);
  1832. if (msg)
  1833. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1834. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1835. "of this file to ftp://upload.libav.org/incoming/ "
  1836. "and contact the libav-devel mailing list.\n");
  1837. va_end(argument_list);
  1838. }
  1839. FF_ENABLE_DEPRECATION_WARNINGS
  1840. #endif /* FF_API_MISSING_SAMPLE */
  1841. static AVHWAccel *first_hwaccel = NULL;
  1842. void av_register_hwaccel(AVHWAccel *hwaccel)
  1843. {
  1844. AVHWAccel **p = &first_hwaccel;
  1845. while (*p)
  1846. p = &(*p)->next;
  1847. *p = hwaccel;
  1848. hwaccel->next = NULL;
  1849. }
  1850. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1851. {
  1852. return hwaccel ? hwaccel->next : first_hwaccel;
  1853. }
  1854. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  1855. {
  1856. AVHWAccel *hwaccel = NULL;
  1857. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1858. if (hwaccel->id == codec_id
  1859. && hwaccel->pix_fmt == pix_fmt)
  1860. return hwaccel;
  1861. return NULL;
  1862. }
  1863. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1864. {
  1865. if (lockmgr_cb) {
  1866. if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1867. return -1;
  1868. if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1869. return -1;
  1870. }
  1871. lockmgr_cb = cb;
  1872. if (lockmgr_cb) {
  1873. if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1874. return -1;
  1875. if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1876. return -1;
  1877. }
  1878. return 0;
  1879. }
  1880. int avpriv_lock_avformat(void)
  1881. {
  1882. if (lockmgr_cb) {
  1883. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1884. return -1;
  1885. }
  1886. return 0;
  1887. }
  1888. int avpriv_unlock_avformat(void)
  1889. {
  1890. if (lockmgr_cb) {
  1891. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1892. return -1;
  1893. }
  1894. return 0;
  1895. }
  1896. unsigned int avpriv_toupper4(unsigned int x)
  1897. {
  1898. return av_toupper(x & 0xFF) +
  1899. (av_toupper((x >> 8) & 0xFF) << 8) +
  1900. (av_toupper((x >> 16) & 0xFF) << 16) +
  1901. (av_toupper((x >> 24) & 0xFF) << 24);
  1902. }
  1903. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  1904. {
  1905. int ret;
  1906. dst->owner = src->owner;
  1907. ret = av_frame_ref(dst->f, src->f);
  1908. if (ret < 0)
  1909. return ret;
  1910. if (src->progress &&
  1911. !(dst->progress = av_buffer_ref(src->progress))) {
  1912. ff_thread_release_buffer(dst->owner, dst);
  1913. return AVERROR(ENOMEM);
  1914. }
  1915. return 0;
  1916. }
  1917. #if !HAVE_THREADS
  1918. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  1919. {
  1920. f->owner = avctx;
  1921. return ff_get_buffer(avctx, f->f, flags);
  1922. }
  1923. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  1924. {
  1925. av_frame_unref(f->f);
  1926. }
  1927. void ff_thread_finish_setup(AVCodecContext *avctx)
  1928. {
  1929. }
  1930. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  1931. {
  1932. }
  1933. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  1934. {
  1935. }
  1936. #endif
  1937. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  1938. {
  1939. if (codec_id <= AV_CODEC_ID_NONE)
  1940. return AVMEDIA_TYPE_UNKNOWN;
  1941. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  1942. return AVMEDIA_TYPE_VIDEO;
  1943. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  1944. return AVMEDIA_TYPE_AUDIO;
  1945. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  1946. return AVMEDIA_TYPE_SUBTITLE;
  1947. return AVMEDIA_TYPE_UNKNOWN;
  1948. }
  1949. int avcodec_is_open(AVCodecContext *s)
  1950. {
  1951. return !!s->internal;
  1952. }
  1953. const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
  1954. const uint8_t *end,
  1955. uint32_t * restrict state)
  1956. {
  1957. int i;
  1958. assert(p <= end);
  1959. if (p >= end)
  1960. return end;
  1961. for (i = 0; i < 3; i++) {
  1962. uint32_t tmp = *state << 8;
  1963. *state = tmp + *(p++);
  1964. if (tmp == 0x100 || p == end)
  1965. return p;
  1966. }
  1967. while (p < end) {
  1968. if (p[-1] > 1 ) p += 3;
  1969. else if (p[-2] ) p += 2;
  1970. else if (p[-3]|(p[-1]-1)) p++;
  1971. else {
  1972. p++;
  1973. break;
  1974. }
  1975. }
  1976. p = FFMIN(p, end) - 4;
  1977. *state = AV_RB32(p);
  1978. return p + 4;
  1979. }