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.

2297 lines
69KB

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