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.

2516 lines
75KB

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