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.

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