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.

2246 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 (*ff_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 (ff_lockmgr_cb) {
  739. if ((*ff_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 (ff_lockmgr_cb) {
  919. (*ff_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. }
  1209. avctx->frame_number++;
  1210. }
  1211. } else
  1212. ret = 0;
  1213. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1214. * make sure it's set correctly */
  1215. picture->extended_data = picture->data;
  1216. return ret;
  1217. }
  1218. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1219. AVFrame *frame,
  1220. int *got_frame_ptr,
  1221. AVPacket *avpkt)
  1222. {
  1223. AVCodecInternal *avci = avctx->internal;
  1224. int planar, channels;
  1225. int ret = 0;
  1226. *got_frame_ptr = 0;
  1227. avctx->pkt = avpkt;
  1228. if (!avpkt->data && avpkt->size) {
  1229. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1230. return AVERROR(EINVAL);
  1231. }
  1232. apply_param_change(avctx, avpkt);
  1233. avcodec_get_frame_defaults(frame);
  1234. if (!avctx->refcounted_frames)
  1235. av_frame_unref(&avci->to_free);
  1236. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1237. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1238. if (ret >= 0 && *got_frame_ptr) {
  1239. avctx->frame_number++;
  1240. frame->pkt_dts = avpkt->dts;
  1241. if (frame->format == AV_SAMPLE_FMT_NONE)
  1242. frame->format = avctx->sample_fmt;
  1243. if (!avctx->refcounted_frames) {
  1244. avci->to_free = *frame;
  1245. avci->to_free.extended_data = avci->to_free.data;
  1246. }
  1247. }
  1248. if (ret < 0 && frame->data[0])
  1249. av_frame_unref(frame);
  1250. }
  1251. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1252. * make sure it's set correctly; assume decoders that actually use
  1253. * extended_data are doing it correctly */
  1254. planar = av_sample_fmt_is_planar(frame->format);
  1255. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1256. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1257. frame->extended_data = frame->data;
  1258. return ret;
  1259. }
  1260. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1261. int *got_sub_ptr,
  1262. AVPacket *avpkt)
  1263. {
  1264. int ret;
  1265. avctx->pkt = avpkt;
  1266. *got_sub_ptr = 0;
  1267. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1268. if (*got_sub_ptr)
  1269. avctx->frame_number++;
  1270. return ret;
  1271. }
  1272. void avsubtitle_free(AVSubtitle *sub)
  1273. {
  1274. int i;
  1275. for (i = 0; i < sub->num_rects; i++) {
  1276. av_freep(&sub->rects[i]->pict.data[0]);
  1277. av_freep(&sub->rects[i]->pict.data[1]);
  1278. av_freep(&sub->rects[i]->pict.data[2]);
  1279. av_freep(&sub->rects[i]->pict.data[3]);
  1280. av_freep(&sub->rects[i]->text);
  1281. av_freep(&sub->rects[i]->ass);
  1282. av_freep(&sub->rects[i]);
  1283. }
  1284. av_freep(&sub->rects);
  1285. memset(sub, 0, sizeof(AVSubtitle));
  1286. }
  1287. av_cold int avcodec_close(AVCodecContext *avctx)
  1288. {
  1289. /* If there is a user-supplied mutex locking routine, call it. */
  1290. if (ff_lockmgr_cb) {
  1291. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1292. return -1;
  1293. }
  1294. entangled_thread_counter++;
  1295. if (entangled_thread_counter != 1) {
  1296. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1297. entangled_thread_counter--;
  1298. return -1;
  1299. }
  1300. if (avcodec_is_open(avctx)) {
  1301. FramePool *pool = avctx->internal->pool;
  1302. int i;
  1303. if (HAVE_THREADS && avctx->thread_opaque)
  1304. ff_thread_free(avctx);
  1305. if (avctx->codec && avctx->codec->close)
  1306. avctx->codec->close(avctx);
  1307. avctx->coded_frame = NULL;
  1308. if (!avctx->refcounted_frames)
  1309. av_frame_unref(&avctx->internal->to_free);
  1310. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  1311. av_buffer_pool_uninit(&pool->pools[i]);
  1312. av_freep(&avctx->internal->pool);
  1313. av_freep(&avctx->internal);
  1314. }
  1315. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1316. av_opt_free(avctx->priv_data);
  1317. av_opt_free(avctx);
  1318. av_freep(&avctx->priv_data);
  1319. if (av_codec_is_encoder(avctx->codec))
  1320. av_freep(&avctx->extradata);
  1321. avctx->codec = NULL;
  1322. avctx->active_thread_type = 0;
  1323. entangled_thread_counter--;
  1324. /* Release any user-supplied mutex. */
  1325. if (ff_lockmgr_cb) {
  1326. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1327. }
  1328. return 0;
  1329. }
  1330. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1331. {
  1332. AVCodec *p, *experimental = NULL;
  1333. p = first_avcodec;
  1334. while (p) {
  1335. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1336. p->id == id) {
  1337. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1338. experimental = p;
  1339. } else
  1340. return p;
  1341. }
  1342. p = p->next;
  1343. }
  1344. return experimental;
  1345. }
  1346. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1347. {
  1348. return find_encdec(id, 1);
  1349. }
  1350. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1351. {
  1352. AVCodec *p;
  1353. if (!name)
  1354. return NULL;
  1355. p = first_avcodec;
  1356. while (p) {
  1357. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1358. return p;
  1359. p = p->next;
  1360. }
  1361. return NULL;
  1362. }
  1363. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1364. {
  1365. return find_encdec(id, 0);
  1366. }
  1367. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1368. {
  1369. AVCodec *p;
  1370. if (!name)
  1371. return NULL;
  1372. p = first_avcodec;
  1373. while (p) {
  1374. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1375. return p;
  1376. p = p->next;
  1377. }
  1378. return NULL;
  1379. }
  1380. static int get_bit_rate(AVCodecContext *ctx)
  1381. {
  1382. int bit_rate;
  1383. int bits_per_sample;
  1384. switch (ctx->codec_type) {
  1385. case AVMEDIA_TYPE_VIDEO:
  1386. case AVMEDIA_TYPE_DATA:
  1387. case AVMEDIA_TYPE_SUBTITLE:
  1388. case AVMEDIA_TYPE_ATTACHMENT:
  1389. bit_rate = ctx->bit_rate;
  1390. break;
  1391. case AVMEDIA_TYPE_AUDIO:
  1392. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1393. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1394. break;
  1395. default:
  1396. bit_rate = 0;
  1397. break;
  1398. }
  1399. return bit_rate;
  1400. }
  1401. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1402. {
  1403. int i, len, ret = 0;
  1404. #define TAG_PRINT(x) \
  1405. (((x) >= '0' && (x) <= '9') || \
  1406. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1407. ((x) == '.' || (x) == ' '))
  1408. for (i = 0; i < 4; i++) {
  1409. len = snprintf(buf, buf_size,
  1410. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1411. buf += len;
  1412. buf_size = buf_size > len ? buf_size - len : 0;
  1413. ret += len;
  1414. codec_tag >>= 8;
  1415. }
  1416. return ret;
  1417. }
  1418. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1419. {
  1420. const char *codec_name;
  1421. const char *profile = NULL;
  1422. const AVCodec *p;
  1423. char buf1[32];
  1424. int bitrate;
  1425. AVRational display_aspect_ratio;
  1426. if (enc->codec)
  1427. p = enc->codec;
  1428. else if (encode)
  1429. p = avcodec_find_encoder(enc->codec_id);
  1430. else
  1431. p = avcodec_find_decoder(enc->codec_id);
  1432. if (p) {
  1433. codec_name = p->name;
  1434. profile = av_get_profile_name(p, enc->profile);
  1435. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1436. /* fake mpeg2 transport stream codec (currently not
  1437. * registered) */
  1438. codec_name = "mpeg2ts";
  1439. } else if (enc->codec_name[0] != '\0') {
  1440. codec_name = enc->codec_name;
  1441. } else {
  1442. /* output avi tags */
  1443. char tag_buf[32];
  1444. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1445. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1446. codec_name = buf1;
  1447. }
  1448. switch (enc->codec_type) {
  1449. case AVMEDIA_TYPE_VIDEO:
  1450. snprintf(buf, buf_size,
  1451. "Video: %s%s",
  1452. codec_name, enc->mb_decision ? " (hq)" : "");
  1453. if (profile)
  1454. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1455. " (%s)", profile);
  1456. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1457. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1458. ", %s",
  1459. av_get_pix_fmt_name(enc->pix_fmt));
  1460. }
  1461. if (enc->width) {
  1462. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1463. ", %dx%d",
  1464. enc->width, enc->height);
  1465. if (enc->sample_aspect_ratio.num) {
  1466. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1467. enc->width * enc->sample_aspect_ratio.num,
  1468. enc->height * enc->sample_aspect_ratio.den,
  1469. 1024 * 1024);
  1470. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1471. " [PAR %d:%d DAR %d:%d]",
  1472. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1473. display_aspect_ratio.num, display_aspect_ratio.den);
  1474. }
  1475. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1476. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1477. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1478. ", %d/%d",
  1479. enc->time_base.num / g, enc->time_base.den / g);
  1480. }
  1481. }
  1482. if (encode) {
  1483. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1484. ", q=%d-%d", enc->qmin, enc->qmax);
  1485. }
  1486. break;
  1487. case AVMEDIA_TYPE_AUDIO:
  1488. snprintf(buf, buf_size,
  1489. "Audio: %s",
  1490. codec_name);
  1491. if (profile)
  1492. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1493. " (%s)", profile);
  1494. if (enc->sample_rate) {
  1495. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1496. ", %d Hz", enc->sample_rate);
  1497. }
  1498. av_strlcat(buf, ", ", buf_size);
  1499. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1500. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1501. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1502. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1503. }
  1504. break;
  1505. case AVMEDIA_TYPE_DATA:
  1506. snprintf(buf, buf_size, "Data: %s", codec_name);
  1507. break;
  1508. case AVMEDIA_TYPE_SUBTITLE:
  1509. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1510. break;
  1511. case AVMEDIA_TYPE_ATTACHMENT:
  1512. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1513. break;
  1514. default:
  1515. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1516. return;
  1517. }
  1518. if (encode) {
  1519. if (enc->flags & CODEC_FLAG_PASS1)
  1520. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1521. ", pass 1");
  1522. if (enc->flags & CODEC_FLAG_PASS2)
  1523. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1524. ", pass 2");
  1525. }
  1526. bitrate = get_bit_rate(enc);
  1527. if (bitrate != 0) {
  1528. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1529. ", %d kb/s", bitrate / 1000);
  1530. }
  1531. }
  1532. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1533. {
  1534. const AVProfile *p;
  1535. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1536. return NULL;
  1537. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1538. if (p->profile == profile)
  1539. return p->name;
  1540. return NULL;
  1541. }
  1542. unsigned avcodec_version(void)
  1543. {
  1544. return LIBAVCODEC_VERSION_INT;
  1545. }
  1546. const char *avcodec_configuration(void)
  1547. {
  1548. return LIBAV_CONFIGURATION;
  1549. }
  1550. const char *avcodec_license(void)
  1551. {
  1552. #define LICENSE_PREFIX "libavcodec license: "
  1553. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1554. }
  1555. void avcodec_flush_buffers(AVCodecContext *avctx)
  1556. {
  1557. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1558. ff_thread_flush(avctx);
  1559. else if (avctx->codec->flush)
  1560. avctx->codec->flush(avctx);
  1561. }
  1562. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1563. {
  1564. switch (codec_id) {
  1565. case AV_CODEC_ID_ADPCM_CT:
  1566. case AV_CODEC_ID_ADPCM_IMA_APC:
  1567. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1568. case AV_CODEC_ID_ADPCM_IMA_WS:
  1569. case AV_CODEC_ID_ADPCM_G722:
  1570. case AV_CODEC_ID_ADPCM_YAMAHA:
  1571. return 4;
  1572. case AV_CODEC_ID_PCM_ALAW:
  1573. case AV_CODEC_ID_PCM_MULAW:
  1574. case AV_CODEC_ID_PCM_S8:
  1575. case AV_CODEC_ID_PCM_U8:
  1576. case AV_CODEC_ID_PCM_ZORK:
  1577. return 8;
  1578. case AV_CODEC_ID_PCM_S16BE:
  1579. case AV_CODEC_ID_PCM_S16LE:
  1580. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1581. case AV_CODEC_ID_PCM_U16BE:
  1582. case AV_CODEC_ID_PCM_U16LE:
  1583. return 16;
  1584. case AV_CODEC_ID_PCM_S24DAUD:
  1585. case AV_CODEC_ID_PCM_S24BE:
  1586. case AV_CODEC_ID_PCM_S24LE:
  1587. case AV_CODEC_ID_PCM_U24BE:
  1588. case AV_CODEC_ID_PCM_U24LE:
  1589. return 24;
  1590. case AV_CODEC_ID_PCM_S32BE:
  1591. case AV_CODEC_ID_PCM_S32LE:
  1592. case AV_CODEC_ID_PCM_U32BE:
  1593. case AV_CODEC_ID_PCM_U32LE:
  1594. case AV_CODEC_ID_PCM_F32BE:
  1595. case AV_CODEC_ID_PCM_F32LE:
  1596. return 32;
  1597. case AV_CODEC_ID_PCM_F64BE:
  1598. case AV_CODEC_ID_PCM_F64LE:
  1599. return 64;
  1600. default:
  1601. return 0;
  1602. }
  1603. }
  1604. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1605. {
  1606. switch (codec_id) {
  1607. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1608. return 2;
  1609. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1610. return 3;
  1611. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1612. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1613. case AV_CODEC_ID_ADPCM_IMA_QT:
  1614. case AV_CODEC_ID_ADPCM_SWF:
  1615. case AV_CODEC_ID_ADPCM_MS:
  1616. return 4;
  1617. default:
  1618. return av_get_exact_bits_per_sample(codec_id);
  1619. }
  1620. }
  1621. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1622. {
  1623. int id, sr, ch, ba, tag, bps;
  1624. id = avctx->codec_id;
  1625. sr = avctx->sample_rate;
  1626. ch = avctx->channels;
  1627. ba = avctx->block_align;
  1628. tag = avctx->codec_tag;
  1629. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1630. /* codecs with an exact constant bits per sample */
  1631. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1632. return (frame_bytes * 8) / (bps * ch);
  1633. bps = avctx->bits_per_coded_sample;
  1634. /* codecs with a fixed packet duration */
  1635. switch (id) {
  1636. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1637. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1638. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1639. case AV_CODEC_ID_AMR_NB:
  1640. case AV_CODEC_ID_GSM:
  1641. case AV_CODEC_ID_QCELP:
  1642. case AV_CODEC_ID_RA_144:
  1643. case AV_CODEC_ID_RA_288: return 160;
  1644. case AV_CODEC_ID_IMC: return 256;
  1645. case AV_CODEC_ID_AMR_WB:
  1646. case AV_CODEC_ID_GSM_MS: return 320;
  1647. case AV_CODEC_ID_MP1: return 384;
  1648. case AV_CODEC_ID_ATRAC1: return 512;
  1649. case AV_CODEC_ID_ATRAC3: return 1024;
  1650. case AV_CODEC_ID_MP2:
  1651. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1652. case AV_CODEC_ID_AC3: return 1536;
  1653. }
  1654. if (sr > 0) {
  1655. /* calc from sample rate */
  1656. if (id == AV_CODEC_ID_TTA)
  1657. return 256 * sr / 245;
  1658. if (ch > 0) {
  1659. /* calc from sample rate and channels */
  1660. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1661. return (480 << (sr / 22050)) / ch;
  1662. }
  1663. }
  1664. if (ba > 0) {
  1665. /* calc from block_align */
  1666. if (id == AV_CODEC_ID_SIPR) {
  1667. switch (ba) {
  1668. case 20: return 160;
  1669. case 19: return 144;
  1670. case 29: return 288;
  1671. case 37: return 480;
  1672. }
  1673. } else if (id == AV_CODEC_ID_ILBC) {
  1674. switch (ba) {
  1675. case 38: return 160;
  1676. case 50: return 240;
  1677. }
  1678. }
  1679. }
  1680. if (frame_bytes > 0) {
  1681. /* calc from frame_bytes only */
  1682. if (id == AV_CODEC_ID_TRUESPEECH)
  1683. return 240 * (frame_bytes / 32);
  1684. if (id == AV_CODEC_ID_NELLYMOSER)
  1685. return 256 * (frame_bytes / 64);
  1686. if (bps > 0) {
  1687. /* calc from frame_bytes and bits_per_coded_sample */
  1688. if (id == AV_CODEC_ID_ADPCM_G726)
  1689. return frame_bytes * 8 / bps;
  1690. }
  1691. if (ch > 0) {
  1692. /* calc from frame_bytes and channels */
  1693. switch (id) {
  1694. case AV_CODEC_ID_ADPCM_4XM:
  1695. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1696. return (frame_bytes - 4 * ch) * 2 / ch;
  1697. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1698. return (frame_bytes - 4) * 2 / ch;
  1699. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1700. return (frame_bytes - 8) * 2 / ch;
  1701. case AV_CODEC_ID_ADPCM_XA:
  1702. return (frame_bytes / 128) * 224 / ch;
  1703. case AV_CODEC_ID_INTERPLAY_DPCM:
  1704. return (frame_bytes - 6 - ch) / ch;
  1705. case AV_CODEC_ID_ROQ_DPCM:
  1706. return (frame_bytes - 8) / ch;
  1707. case AV_CODEC_ID_XAN_DPCM:
  1708. return (frame_bytes - 2 * ch) / ch;
  1709. case AV_CODEC_ID_MACE3:
  1710. return 3 * frame_bytes / ch;
  1711. case AV_CODEC_ID_MACE6:
  1712. return 6 * frame_bytes / ch;
  1713. case AV_CODEC_ID_PCM_LXF:
  1714. return 2 * (frame_bytes / (5 * ch));
  1715. }
  1716. if (tag) {
  1717. /* calc from frame_bytes, channels, and codec_tag */
  1718. if (id == AV_CODEC_ID_SOL_DPCM) {
  1719. if (tag == 3)
  1720. return frame_bytes / ch;
  1721. else
  1722. return frame_bytes * 2 / ch;
  1723. }
  1724. }
  1725. if (ba > 0) {
  1726. /* calc from frame_bytes, channels, and block_align */
  1727. int blocks = frame_bytes / ba;
  1728. switch (avctx->codec_id) {
  1729. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1730. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1731. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1732. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1733. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1734. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1735. case AV_CODEC_ID_ADPCM_MS:
  1736. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1737. }
  1738. }
  1739. if (bps > 0) {
  1740. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1741. switch (avctx->codec_id) {
  1742. case AV_CODEC_ID_PCM_DVD:
  1743. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1744. case AV_CODEC_ID_PCM_BLURAY:
  1745. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1746. case AV_CODEC_ID_S302M:
  1747. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1748. }
  1749. }
  1750. }
  1751. }
  1752. return 0;
  1753. }
  1754. #if !HAVE_THREADS
  1755. int ff_thread_init(AVCodecContext *s)
  1756. {
  1757. return -1;
  1758. }
  1759. #endif
  1760. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1761. {
  1762. unsigned int n = 0;
  1763. while (v >= 0xff) {
  1764. *s++ = 0xff;
  1765. v -= 0xff;
  1766. n++;
  1767. }
  1768. *s = v;
  1769. n++;
  1770. return n;
  1771. }
  1772. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1773. {
  1774. int i;
  1775. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1776. return i;
  1777. }
  1778. #if FF_API_MISSING_SAMPLE
  1779. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1780. {
  1781. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
  1782. "version to the newest one from Git. If the problem still "
  1783. "occurs, it means that your file has a feature which has not "
  1784. "been implemented.\n", feature);
  1785. if(want_sample)
  1786. av_log_ask_for_sample(avc, NULL);
  1787. }
  1788. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1789. {
  1790. va_list argument_list;
  1791. va_start(argument_list, msg);
  1792. if (msg)
  1793. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1794. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1795. "of this file to ftp://upload.libav.org/incoming/ "
  1796. "and contact the libav-devel mailing list.\n");
  1797. va_end(argument_list);
  1798. }
  1799. #endif /* FF_API_MISSING_SAMPLE */
  1800. static AVHWAccel *first_hwaccel = NULL;
  1801. void av_register_hwaccel(AVHWAccel *hwaccel)
  1802. {
  1803. AVHWAccel **p = &first_hwaccel;
  1804. while (*p)
  1805. p = &(*p)->next;
  1806. *p = hwaccel;
  1807. hwaccel->next = NULL;
  1808. }
  1809. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1810. {
  1811. return hwaccel ? hwaccel->next : first_hwaccel;
  1812. }
  1813. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  1814. {
  1815. AVHWAccel *hwaccel = NULL;
  1816. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1817. if (hwaccel->id == codec_id
  1818. && hwaccel->pix_fmt == pix_fmt)
  1819. return hwaccel;
  1820. return NULL;
  1821. }
  1822. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1823. {
  1824. if (ff_lockmgr_cb) {
  1825. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1826. return -1;
  1827. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1828. return -1;
  1829. }
  1830. ff_lockmgr_cb = cb;
  1831. if (ff_lockmgr_cb) {
  1832. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1833. return -1;
  1834. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1835. return -1;
  1836. }
  1837. return 0;
  1838. }
  1839. int avpriv_lock_avformat(void)
  1840. {
  1841. if (ff_lockmgr_cb) {
  1842. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1843. return -1;
  1844. }
  1845. return 0;
  1846. }
  1847. int avpriv_unlock_avformat(void)
  1848. {
  1849. if (ff_lockmgr_cb) {
  1850. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1851. return -1;
  1852. }
  1853. return 0;
  1854. }
  1855. unsigned int avpriv_toupper4(unsigned int x)
  1856. {
  1857. return av_toupper(x & 0xFF) +
  1858. (av_toupper((x >> 8) & 0xFF) << 8) +
  1859. (av_toupper((x >> 16) & 0xFF) << 16) +
  1860. (av_toupper((x >> 24) & 0xFF) << 24);
  1861. }
  1862. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  1863. {
  1864. int ret;
  1865. dst->owner = src->owner;
  1866. ret = av_frame_ref(dst->f, src->f);
  1867. if (ret < 0)
  1868. return ret;
  1869. if (src->progress &&
  1870. !(dst->progress = av_buffer_ref(src->progress))) {
  1871. ff_thread_release_buffer(dst->owner, dst);
  1872. return AVERROR(ENOMEM);
  1873. }
  1874. return 0;
  1875. }
  1876. #if !HAVE_THREADS
  1877. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  1878. {
  1879. f->owner = avctx;
  1880. return ff_get_buffer(avctx, f->f, flags);
  1881. }
  1882. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  1883. {
  1884. av_frame_unref(f->f);
  1885. }
  1886. void ff_thread_finish_setup(AVCodecContext *avctx)
  1887. {
  1888. }
  1889. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  1890. {
  1891. }
  1892. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  1893. {
  1894. }
  1895. #endif
  1896. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  1897. {
  1898. if (codec_id <= AV_CODEC_ID_NONE)
  1899. return AVMEDIA_TYPE_UNKNOWN;
  1900. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  1901. return AVMEDIA_TYPE_VIDEO;
  1902. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  1903. return AVMEDIA_TYPE_AUDIO;
  1904. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  1905. return AVMEDIA_TYPE_SUBTITLE;
  1906. return AVMEDIA_TYPE_UNKNOWN;
  1907. }
  1908. int avcodec_is_open(AVCodecContext *s)
  1909. {
  1910. return !!s->internal;
  1911. }
  1912. const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
  1913. const uint8_t *end,
  1914. uint32_t * restrict state)
  1915. {
  1916. int i;
  1917. assert(p <= end);
  1918. if (p >= end)
  1919. return end;
  1920. for (i = 0; i < 3; i++) {
  1921. uint32_t tmp = *state << 8;
  1922. *state = tmp + *(p++);
  1923. if (tmp == 0x100 || p == end)
  1924. return p;
  1925. }
  1926. while (p < end) {
  1927. if (p[-1] > 1 ) p += 3;
  1928. else if (p[-2] ) p += 2;
  1929. else if (p[-3]|(p[-1]-1)) p++;
  1930. else {
  1931. p++;
  1932. break;
  1933. }
  1934. }
  1935. p = FFMIN(p, end) - 4;
  1936. *state = AV_RB32(p);
  1937. return p + 4;
  1938. }