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.

2450 lines
73KB

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