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.

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