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.

2200 lines
66KB

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