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.

2336 lines
70KB

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