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.

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