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.

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