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.

2431 lines
72KB

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