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.

2279 lines
68KB

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