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.

2531 lines
76KB

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