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.

2572 lines
77KB

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