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.

2250 lines
67KB

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