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.

2237 lines
67KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "config.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/channel_layout.h"
  30. #include "libavutil/crc.h"
  31. #include "libavutil/frame.h"
  32. #include "libavutil/mathematics.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavutil/samplefmt.h"
  36. #include "libavutil/dict.h"
  37. #include "avcodec.h"
  38. #include "dsputil.h"
  39. #include "libavutil/opt.h"
  40. #include "thread.h"
  41. #include "internal.h"
  42. #include "bytestream.h"
  43. #include "version.h"
  44. #include <stdlib.h>
  45. #include <stdarg.h>
  46. #include <limits.h>
  47. #include <float.h>
  48. static int volatile entangled_thread_counter = 0;
  49. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  50. static void *codec_mutex;
  51. static void *avformat_mutex;
  52. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  53. {
  54. if (min_size < *size)
  55. return ptr;
  56. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  57. ptr = av_realloc(ptr, min_size);
  58. /* we could set this to the unmodified min_size but this is safer
  59. * if the user lost the ptr and uses NULL now
  60. */
  61. if (!ptr)
  62. min_size = 0;
  63. *size = min_size;
  64. return ptr;
  65. }
  66. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  67. {
  68. void **p = ptr;
  69. if (min_size < *size)
  70. return;
  71. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  72. av_free(*p);
  73. *p = av_malloc(min_size);
  74. if (!*p)
  75. min_size = 0;
  76. *size = min_size;
  77. }
  78. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  79. {
  80. void **p = ptr;
  81. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  82. av_freep(p);
  83. *size = 0;
  84. return;
  85. }
  86. av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  87. if (*size)
  88. memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  89. }
  90. /* encoder management */
  91. static AVCodec *first_avcodec = NULL;
  92. AVCodec *av_codec_next(const AVCodec *c)
  93. {
  94. if (c)
  95. return c->next;
  96. else
  97. return first_avcodec;
  98. }
  99. static void avcodec_init(void)
  100. {
  101. static int initialized = 0;
  102. if (initialized != 0)
  103. return;
  104. initialized = 1;
  105. 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. /* workaround for AVHWAccel plane count of 0, buf[0] is used as
  578. check for allocated buffers: make libavcodec happy */
  579. if (desc && desc->flags & PIX_FMT_HWACCEL)
  580. planes = 1;
  581. if (!desc || planes <= 0) {
  582. ret = AVERROR(EINVAL);
  583. goto fail;
  584. }
  585. for (i = 0; i < planes; i++) {
  586. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  587. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  588. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  589. }
  590. } else {
  591. int planar = av_sample_fmt_is_planar(frame->format);
  592. planes = planar ? avctx->channels : 1;
  593. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  594. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  595. frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
  596. frame->nb_extended_buf);
  597. if (!frame->extended_buf) {
  598. ret = AVERROR(ENOMEM);
  599. goto fail;
  600. }
  601. }
  602. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  603. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  604. for (i = 0; i < frame->nb_extended_buf; i++)
  605. WRAP_PLANE(frame->extended_buf[i],
  606. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  607. frame->linesize[0]);
  608. }
  609. av_buffer_unref(&dummy_buf);
  610. return 0;
  611. fail:
  612. avctx->release_buffer(avctx, frame);
  613. av_freep(&priv);
  614. av_buffer_unref(&dummy_buf);
  615. return ret;
  616. }
  617. #endif
  618. return avctx->get_buffer2(avctx, frame, flags);
  619. }
  620. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  621. {
  622. AVFrame tmp;
  623. int ret;
  624. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  625. if (!frame->data[0])
  626. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  627. if (av_frame_is_writable(frame))
  628. return 0;
  629. av_frame_move_ref(&tmp, frame);
  630. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  631. if (ret < 0) {
  632. av_frame_unref(&tmp);
  633. return ret;
  634. }
  635. av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
  636. frame->format, frame->width, frame->height);
  637. av_frame_unref(&tmp);
  638. return 0;
  639. }
  640. #if FF_API_GET_BUFFER
  641. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  642. {
  643. av_frame_unref(pic);
  644. }
  645. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  646. {
  647. av_assert0(0);
  648. }
  649. #endif
  650. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  651. {
  652. int i;
  653. for (i = 0; i < count; i++) {
  654. int r = func(c, (char *)arg + i * size);
  655. if (ret)
  656. ret[i] = r;
  657. }
  658. return 0;
  659. }
  660. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  661. {
  662. int i;
  663. for (i = 0; i < count; i++) {
  664. int r = func(c, arg, i, 0);
  665. if (ret)
  666. ret[i] = r;
  667. }
  668. return 0;
  669. }
  670. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  671. {
  672. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  673. return desc->flags & PIX_FMT_HWACCEL;
  674. }
  675. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  676. {
  677. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  678. ++fmt;
  679. return fmt[0];
  680. }
  681. void avcodec_get_frame_defaults(AVFrame *frame)
  682. {
  683. if (frame->extended_data != frame->data)
  684. av_freep(&frame->extended_data);
  685. memset(frame, 0, sizeof(AVFrame));
  686. frame->pts = AV_NOPTS_VALUE;
  687. frame->key_frame = 1;
  688. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  689. frame->format = -1; /* unknown */
  690. frame->extended_data = frame->data;
  691. }
  692. AVFrame *avcodec_alloc_frame(void)
  693. {
  694. AVFrame *frame = av_mallocz(sizeof(AVFrame));
  695. if (frame == NULL)
  696. return NULL;
  697. avcodec_get_frame_defaults(frame);
  698. return frame;
  699. }
  700. void avcodec_free_frame(AVFrame **frame)
  701. {
  702. AVFrame *f;
  703. if (!frame || !*frame)
  704. return;
  705. f = *frame;
  706. if (f->extended_data != f->data)
  707. av_freep(&f->extended_data);
  708. av_freep(frame);
  709. }
  710. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  711. {
  712. int ret = 0;
  713. AVDictionary *tmp = NULL;
  714. if (avcodec_is_open(avctx))
  715. return 0;
  716. if ((!codec && !avctx->codec)) {
  717. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  718. return AVERROR(EINVAL);
  719. }
  720. if ((codec && avctx->codec && codec != avctx->codec)) {
  721. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  722. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  723. return AVERROR(EINVAL);
  724. }
  725. if (!codec)
  726. codec = avctx->codec;
  727. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  728. return AVERROR(EINVAL);
  729. if (options)
  730. av_dict_copy(&tmp, *options, 0);
  731. /* If there is a user-supplied mutex locking routine, call it. */
  732. if (ff_lockmgr_cb) {
  733. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  734. return -1;
  735. }
  736. entangled_thread_counter++;
  737. if (entangled_thread_counter != 1) {
  738. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  739. ret = -1;
  740. goto end;
  741. }
  742. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  743. if (!avctx->internal) {
  744. ret = AVERROR(ENOMEM);
  745. goto end;
  746. }
  747. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  748. if (!avctx->internal->pool) {
  749. ret = AVERROR(ENOMEM);
  750. goto free_and_end;
  751. }
  752. if (codec->priv_data_size > 0) {
  753. if (!avctx->priv_data) {
  754. avctx->priv_data = av_mallocz(codec->priv_data_size);
  755. if (!avctx->priv_data) {
  756. ret = AVERROR(ENOMEM);
  757. goto end;
  758. }
  759. if (codec->priv_class) {
  760. *(const AVClass **)avctx->priv_data = codec->priv_class;
  761. av_opt_set_defaults(avctx->priv_data);
  762. }
  763. }
  764. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  765. goto free_and_end;
  766. } else {
  767. avctx->priv_data = NULL;
  768. }
  769. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  770. goto free_and_end;
  771. if (avctx->coded_width && avctx->coded_height)
  772. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  773. else if (avctx->width && avctx->height)
  774. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  775. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  776. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  777. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  778. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  779. avcodec_set_dimensions(avctx, 0, 0);
  780. }
  781. /* if the decoder init function was already called previously,
  782. * free the already allocated subtitle_header before overwriting it */
  783. if (av_codec_is_decoder(codec))
  784. av_freep(&avctx->subtitle_header);
  785. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  786. ret = AVERROR(EINVAL);
  787. goto free_and_end;
  788. }
  789. avctx->codec = codec;
  790. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  791. avctx->codec_id == AV_CODEC_ID_NONE) {
  792. avctx->codec_type = codec->type;
  793. avctx->codec_id = codec->id;
  794. }
  795. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  796. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  797. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  798. ret = AVERROR(EINVAL);
  799. goto free_and_end;
  800. }
  801. avctx->frame_number = 0;
  802. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  803. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  804. ret = AVERROR_EXPERIMENTAL;
  805. goto free_and_end;
  806. }
  807. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  808. (!avctx->time_base.num || !avctx->time_base.den)) {
  809. avctx->time_base.num = 1;
  810. avctx->time_base.den = avctx->sample_rate;
  811. }
  812. if (HAVE_THREADS && !avctx->thread_opaque) {
  813. ret = ff_thread_init(avctx);
  814. if (ret < 0) {
  815. goto free_and_end;
  816. }
  817. }
  818. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  819. avctx->thread_count = 1;
  820. if (av_codec_is_encoder(avctx->codec)) {
  821. int i;
  822. if (avctx->codec->sample_fmts) {
  823. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  824. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  825. break;
  826. if (avctx->channels == 1 &&
  827. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  828. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  829. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  830. break;
  831. }
  832. }
  833. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  834. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  835. ret = AVERROR(EINVAL);
  836. goto free_and_end;
  837. }
  838. }
  839. if (avctx->codec->pix_fmts) {
  840. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  841. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  842. break;
  843. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
  844. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  845. ret = AVERROR(EINVAL);
  846. goto free_and_end;
  847. }
  848. }
  849. if (avctx->codec->supported_samplerates) {
  850. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  851. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  852. break;
  853. if (avctx->codec->supported_samplerates[i] == 0) {
  854. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  855. ret = AVERROR(EINVAL);
  856. goto free_and_end;
  857. }
  858. }
  859. if (avctx->codec->channel_layouts) {
  860. if (!avctx->channel_layout) {
  861. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  862. } else {
  863. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  864. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  865. break;
  866. if (avctx->codec->channel_layouts[i] == 0) {
  867. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  868. ret = AVERROR(EINVAL);
  869. goto free_and_end;
  870. }
  871. }
  872. }
  873. if (avctx->channel_layout && avctx->channels) {
  874. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  875. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  876. ret = AVERROR(EINVAL);
  877. goto free_and_end;
  878. }
  879. } else if (avctx->channel_layout) {
  880. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  881. }
  882. if (!avctx->rc_initial_buffer_occupancy)
  883. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  884. }
  885. if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  886. ret = avctx->codec->init(avctx);
  887. if (ret < 0) {
  888. goto free_and_end;
  889. }
  890. }
  891. if (av_codec_is_decoder(avctx->codec)) {
  892. /* validate channel layout from the decoder */
  893. if (avctx->channel_layout) {
  894. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  895. if (!avctx->channels)
  896. avctx->channels = channels;
  897. else if (channels != avctx->channels) {
  898. av_log(avctx, AV_LOG_WARNING,
  899. "channel layout does not match number of channels\n");
  900. avctx->channel_layout = 0;
  901. }
  902. }
  903. if (avctx->channels && avctx->channels < 0 ||
  904. avctx->channels > FF_SANE_NB_CHANNELS) {
  905. ret = AVERROR(EINVAL);
  906. goto free_and_end;
  907. }
  908. }
  909. end:
  910. entangled_thread_counter--;
  911. /* Release any user-supplied mutex. */
  912. if (ff_lockmgr_cb) {
  913. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  914. }
  915. if (options) {
  916. av_dict_free(options);
  917. *options = tmp;
  918. }
  919. return ret;
  920. free_and_end:
  921. av_dict_free(&tmp);
  922. av_freep(&avctx->priv_data);
  923. if (avctx->internal)
  924. av_freep(&avctx->internal->pool);
  925. av_freep(&avctx->internal);
  926. avctx->codec = NULL;
  927. goto end;
  928. }
  929. int ff_alloc_packet(AVPacket *avpkt, int size)
  930. {
  931. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  932. return AVERROR(EINVAL);
  933. if (avpkt->data) {
  934. AVBufferRef *buf = avpkt->buf;
  935. #if FF_API_DESTRUCT_PACKET
  936. void *destruct = avpkt->destruct;
  937. #endif
  938. if (avpkt->size < size)
  939. return AVERROR(EINVAL);
  940. av_init_packet(avpkt);
  941. #if FF_API_DESTRUCT_PACKET
  942. avpkt->destruct = destruct;
  943. #endif
  944. avpkt->buf = buf;
  945. avpkt->size = size;
  946. return 0;
  947. } else {
  948. return av_new_packet(avpkt, size);
  949. }
  950. }
  951. /**
  952. * Pad last frame with silence.
  953. */
  954. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  955. {
  956. AVFrame *frame = NULL;
  957. uint8_t *buf = NULL;
  958. int ret;
  959. if (!(frame = avcodec_alloc_frame()))
  960. return AVERROR(ENOMEM);
  961. *frame = *src;
  962. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  963. s->frame_size, s->sample_fmt, 0)) < 0)
  964. goto fail;
  965. if (!(buf = av_malloc(ret))) {
  966. ret = AVERROR(ENOMEM);
  967. goto fail;
  968. }
  969. frame->nb_samples = s->frame_size;
  970. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  971. buf, ret, 0)) < 0)
  972. goto fail;
  973. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  974. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  975. goto fail;
  976. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  977. frame->nb_samples - src->nb_samples,
  978. s->channels, s->sample_fmt)) < 0)
  979. goto fail;
  980. *dst = frame;
  981. return 0;
  982. fail:
  983. if (frame->extended_data != frame->data)
  984. av_freep(&frame->extended_data);
  985. av_freep(&buf);
  986. av_freep(&frame);
  987. return ret;
  988. }
  989. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  990. AVPacket *avpkt,
  991. const AVFrame *frame,
  992. int *got_packet_ptr)
  993. {
  994. AVFrame tmp;
  995. AVFrame *padded_frame = NULL;
  996. int ret;
  997. int user_packet = !!avpkt->data;
  998. *got_packet_ptr = 0;
  999. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1000. av_free_packet(avpkt);
  1001. av_init_packet(avpkt);
  1002. return 0;
  1003. }
  1004. /* ensure that extended_data is properly set */
  1005. if (frame && !frame->extended_data) {
  1006. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1007. avctx->channels > AV_NUM_DATA_POINTERS) {
  1008. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1009. "with more than %d channels, but extended_data is not set.\n",
  1010. AV_NUM_DATA_POINTERS);
  1011. return AVERROR(EINVAL);
  1012. }
  1013. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1014. tmp = *frame;
  1015. tmp.extended_data = tmp.data;
  1016. frame = &tmp;
  1017. }
  1018. /* check for valid frame size */
  1019. if (frame) {
  1020. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1021. if (frame->nb_samples > avctx->frame_size)
  1022. return AVERROR(EINVAL);
  1023. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1024. if (frame->nb_samples < avctx->frame_size &&
  1025. !avctx->internal->last_audio_frame) {
  1026. ret = pad_last_frame(avctx, &padded_frame, frame);
  1027. if (ret < 0)
  1028. return ret;
  1029. frame = padded_frame;
  1030. avctx->internal->last_audio_frame = 1;
  1031. }
  1032. if (frame->nb_samples != avctx->frame_size) {
  1033. ret = AVERROR(EINVAL);
  1034. goto end;
  1035. }
  1036. }
  1037. }
  1038. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1039. if (!ret) {
  1040. if (*got_packet_ptr) {
  1041. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1042. if (avpkt->pts == AV_NOPTS_VALUE)
  1043. avpkt->pts = frame->pts;
  1044. if (!avpkt->duration)
  1045. avpkt->duration = ff_samples_to_time_base(avctx,
  1046. frame->nb_samples);
  1047. }
  1048. avpkt->dts = avpkt->pts;
  1049. } else {
  1050. avpkt->size = 0;
  1051. }
  1052. if (!user_packet && avpkt->size) {
  1053. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  1054. if (ret >= 0)
  1055. avpkt->data = avpkt->buf->data;
  1056. }
  1057. avctx->frame_number++;
  1058. }
  1059. if (ret < 0 || !*got_packet_ptr) {
  1060. av_free_packet(avpkt);
  1061. av_init_packet(avpkt);
  1062. goto end;
  1063. }
  1064. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1065. * this needs to be moved to the encoders, but for now we can do it
  1066. * here to simplify things */
  1067. avpkt->flags |= AV_PKT_FLAG_KEY;
  1068. end:
  1069. if (padded_frame) {
  1070. av_freep(&padded_frame->data[0]);
  1071. if (padded_frame->extended_data != padded_frame->data)
  1072. av_freep(&padded_frame->extended_data);
  1073. av_freep(&padded_frame);
  1074. }
  1075. return ret;
  1076. }
  1077. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1078. AVPacket *avpkt,
  1079. const AVFrame *frame,
  1080. int *got_packet_ptr)
  1081. {
  1082. int ret;
  1083. int user_packet = !!avpkt->data;
  1084. *got_packet_ptr = 0;
  1085. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1086. av_free_packet(avpkt);
  1087. av_init_packet(avpkt);
  1088. avpkt->size = 0;
  1089. return 0;
  1090. }
  1091. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1092. return AVERROR(EINVAL);
  1093. av_assert0(avctx->codec->encode2);
  1094. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1095. if (!ret) {
  1096. if (!*got_packet_ptr)
  1097. avpkt->size = 0;
  1098. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1099. avpkt->pts = avpkt->dts = frame->pts;
  1100. if (!user_packet && avpkt->size) {
  1101. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  1102. if (ret >= 0)
  1103. avpkt->data = avpkt->buf->data;
  1104. }
  1105. avctx->frame_number++;
  1106. }
  1107. if (ret < 0 || !*got_packet_ptr)
  1108. av_free_packet(avpkt);
  1109. emms_c();
  1110. return ret;
  1111. }
  1112. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1113. const AVSubtitle *sub)
  1114. {
  1115. int ret;
  1116. if (sub->start_display_time) {
  1117. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1118. return -1;
  1119. }
  1120. if (sub->num_rects == 0 || !sub->rects)
  1121. return -1;
  1122. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1123. avctx->frame_number++;
  1124. return ret;
  1125. }
  1126. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1127. {
  1128. int size = 0;
  1129. const uint8_t *data;
  1130. uint32_t flags;
  1131. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1132. return;
  1133. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1134. if (!data || size < 4)
  1135. return;
  1136. flags = bytestream_get_le32(&data);
  1137. size -= 4;
  1138. if (size < 4) /* Required for any of the changes */
  1139. return;
  1140. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1141. avctx->channels = bytestream_get_le32(&data);
  1142. size -= 4;
  1143. }
  1144. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1145. if (size < 8)
  1146. return;
  1147. avctx->channel_layout = bytestream_get_le64(&data);
  1148. size -= 8;
  1149. }
  1150. if (size < 4)
  1151. return;
  1152. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1153. avctx->sample_rate = bytestream_get_le32(&data);
  1154. size -= 4;
  1155. }
  1156. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1157. if (size < 8)
  1158. return;
  1159. avctx->width = bytestream_get_le32(&data);
  1160. avctx->height = bytestream_get_le32(&data);
  1161. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1162. size -= 8;
  1163. }
  1164. }
  1165. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1166. int *got_picture_ptr,
  1167. AVPacket *avpkt)
  1168. {
  1169. AVCodecInternal *avci = avctx->internal;
  1170. int ret;
  1171. *got_picture_ptr = 0;
  1172. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1173. return -1;
  1174. avctx->pkt = avpkt;
  1175. apply_param_change(avctx, avpkt);
  1176. avcodec_get_frame_defaults(picture);
  1177. if (!avctx->refcounted_frames)
  1178. av_frame_unref(&avci->to_free);
  1179. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1180. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1181. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1182. avpkt);
  1183. else {
  1184. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1185. avpkt);
  1186. picture->pkt_dts = avpkt->dts;
  1187. /* get_buffer is supposed to set frame parameters */
  1188. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1189. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1190. picture->width = avctx->width;
  1191. picture->height = avctx->height;
  1192. picture->format = avctx->pix_fmt;
  1193. }
  1194. }
  1195. emms_c(); //needed to avoid an emms_c() call before every return;
  1196. if (ret < 0 && picture->data[0])
  1197. av_frame_unref(picture);
  1198. if (*got_picture_ptr) {
  1199. if (!avctx->refcounted_frames) {
  1200. avci->to_free = *picture;
  1201. avci->to_free.extended_data = avci->to_free.data;
  1202. }
  1203. avctx->frame_number++;
  1204. }
  1205. } else
  1206. ret = 0;
  1207. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1208. * make sure it's set correctly */
  1209. picture->extended_data = picture->data;
  1210. return ret;
  1211. }
  1212. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1213. AVFrame *frame,
  1214. int *got_frame_ptr,
  1215. AVPacket *avpkt)
  1216. {
  1217. AVCodecInternal *avci = avctx->internal;
  1218. int planar, channels;
  1219. int ret = 0;
  1220. *got_frame_ptr = 0;
  1221. avctx->pkt = avpkt;
  1222. if (!avpkt->data && avpkt->size) {
  1223. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1224. return AVERROR(EINVAL);
  1225. }
  1226. apply_param_change(avctx, avpkt);
  1227. avcodec_get_frame_defaults(frame);
  1228. if (!avctx->refcounted_frames)
  1229. av_frame_unref(&avci->to_free);
  1230. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1231. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1232. if (ret >= 0 && *got_frame_ptr) {
  1233. avctx->frame_number++;
  1234. frame->pkt_dts = avpkt->dts;
  1235. if (frame->format == AV_SAMPLE_FMT_NONE)
  1236. frame->format = avctx->sample_fmt;
  1237. if (!avctx->refcounted_frames) {
  1238. avci->to_free = *frame;
  1239. avci->to_free.extended_data = avci->to_free.data;
  1240. }
  1241. }
  1242. if (ret < 0 && frame->data[0])
  1243. av_frame_unref(frame);
  1244. }
  1245. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1246. * make sure it's set correctly; assume decoders that actually use
  1247. * extended_data are doing it correctly */
  1248. planar = av_sample_fmt_is_planar(frame->format);
  1249. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1250. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1251. frame->extended_data = frame->data;
  1252. return ret;
  1253. }
  1254. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1255. int *got_sub_ptr,
  1256. AVPacket *avpkt)
  1257. {
  1258. int ret;
  1259. avctx->pkt = avpkt;
  1260. *got_sub_ptr = 0;
  1261. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1262. if (*got_sub_ptr)
  1263. avctx->frame_number++;
  1264. return ret;
  1265. }
  1266. void avsubtitle_free(AVSubtitle *sub)
  1267. {
  1268. int i;
  1269. for (i = 0; i < sub->num_rects; i++) {
  1270. av_freep(&sub->rects[i]->pict.data[0]);
  1271. av_freep(&sub->rects[i]->pict.data[1]);
  1272. av_freep(&sub->rects[i]->pict.data[2]);
  1273. av_freep(&sub->rects[i]->pict.data[3]);
  1274. av_freep(&sub->rects[i]->text);
  1275. av_freep(&sub->rects[i]->ass);
  1276. av_freep(&sub->rects[i]);
  1277. }
  1278. av_freep(&sub->rects);
  1279. memset(sub, 0, sizeof(AVSubtitle));
  1280. }
  1281. av_cold int avcodec_close(AVCodecContext *avctx)
  1282. {
  1283. /* If there is a user-supplied mutex locking routine, call it. */
  1284. if (ff_lockmgr_cb) {
  1285. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1286. return -1;
  1287. }
  1288. entangled_thread_counter++;
  1289. if (entangled_thread_counter != 1) {
  1290. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1291. entangled_thread_counter--;
  1292. return -1;
  1293. }
  1294. if (avcodec_is_open(avctx)) {
  1295. FramePool *pool = avctx->internal->pool;
  1296. int i;
  1297. if (HAVE_THREADS && avctx->thread_opaque)
  1298. ff_thread_free(avctx);
  1299. if (avctx->codec && avctx->codec->close)
  1300. avctx->codec->close(avctx);
  1301. avctx->coded_frame = NULL;
  1302. if (!avctx->refcounted_frames)
  1303. av_frame_unref(&avctx->internal->to_free);
  1304. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  1305. av_buffer_pool_uninit(&pool->pools[i]);
  1306. av_freep(&avctx->internal->pool);
  1307. av_freep(&avctx->internal);
  1308. }
  1309. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1310. av_opt_free(avctx->priv_data);
  1311. av_opt_free(avctx);
  1312. av_freep(&avctx->priv_data);
  1313. if (av_codec_is_encoder(avctx->codec))
  1314. av_freep(&avctx->extradata);
  1315. avctx->codec = NULL;
  1316. avctx->active_thread_type = 0;
  1317. entangled_thread_counter--;
  1318. /* Release any user-supplied mutex. */
  1319. if (ff_lockmgr_cb) {
  1320. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1321. }
  1322. return 0;
  1323. }
  1324. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1325. {
  1326. AVCodec *p, *experimental = NULL;
  1327. p = first_avcodec;
  1328. while (p) {
  1329. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1330. p->id == id) {
  1331. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1332. experimental = p;
  1333. } else
  1334. return p;
  1335. }
  1336. p = p->next;
  1337. }
  1338. return experimental;
  1339. }
  1340. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1341. {
  1342. return find_encdec(id, 1);
  1343. }
  1344. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1345. {
  1346. AVCodec *p;
  1347. if (!name)
  1348. return NULL;
  1349. p = first_avcodec;
  1350. while (p) {
  1351. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1352. return p;
  1353. p = p->next;
  1354. }
  1355. return NULL;
  1356. }
  1357. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1358. {
  1359. return find_encdec(id, 0);
  1360. }
  1361. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1362. {
  1363. AVCodec *p;
  1364. if (!name)
  1365. return NULL;
  1366. p = first_avcodec;
  1367. while (p) {
  1368. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1369. return p;
  1370. p = p->next;
  1371. }
  1372. return NULL;
  1373. }
  1374. static int get_bit_rate(AVCodecContext *ctx)
  1375. {
  1376. int bit_rate;
  1377. int bits_per_sample;
  1378. switch (ctx->codec_type) {
  1379. case AVMEDIA_TYPE_VIDEO:
  1380. case AVMEDIA_TYPE_DATA:
  1381. case AVMEDIA_TYPE_SUBTITLE:
  1382. case AVMEDIA_TYPE_ATTACHMENT:
  1383. bit_rate = ctx->bit_rate;
  1384. break;
  1385. case AVMEDIA_TYPE_AUDIO:
  1386. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1387. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1388. break;
  1389. default:
  1390. bit_rate = 0;
  1391. break;
  1392. }
  1393. return bit_rate;
  1394. }
  1395. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1396. {
  1397. int i, len, ret = 0;
  1398. #define TAG_PRINT(x) \
  1399. (((x) >= '0' && (x) <= '9') || \
  1400. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1401. ((x) == '.' || (x) == ' '))
  1402. for (i = 0; i < 4; i++) {
  1403. len = snprintf(buf, buf_size,
  1404. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1405. buf += len;
  1406. buf_size = buf_size > len ? buf_size - len : 0;
  1407. ret += len;
  1408. codec_tag >>= 8;
  1409. }
  1410. return ret;
  1411. }
  1412. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1413. {
  1414. const char *codec_name;
  1415. const char *profile = NULL;
  1416. const AVCodec *p;
  1417. char buf1[32];
  1418. int bitrate;
  1419. AVRational display_aspect_ratio;
  1420. if (enc->codec)
  1421. p = enc->codec;
  1422. else if (encode)
  1423. p = avcodec_find_encoder(enc->codec_id);
  1424. else
  1425. p = avcodec_find_decoder(enc->codec_id);
  1426. if (p) {
  1427. codec_name = p->name;
  1428. profile = av_get_profile_name(p, enc->profile);
  1429. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1430. /* fake mpeg2 transport stream codec (currently not
  1431. * registered) */
  1432. codec_name = "mpeg2ts";
  1433. } else if (enc->codec_name[0] != '\0') {
  1434. codec_name = enc->codec_name;
  1435. } else {
  1436. /* output avi tags */
  1437. char tag_buf[32];
  1438. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1439. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1440. codec_name = buf1;
  1441. }
  1442. switch (enc->codec_type) {
  1443. case AVMEDIA_TYPE_VIDEO:
  1444. snprintf(buf, buf_size,
  1445. "Video: %s%s",
  1446. codec_name, enc->mb_decision ? " (hq)" : "");
  1447. if (profile)
  1448. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1449. " (%s)", profile);
  1450. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1451. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1452. ", %s",
  1453. av_get_pix_fmt_name(enc->pix_fmt));
  1454. }
  1455. if (enc->width) {
  1456. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1457. ", %dx%d",
  1458. enc->width, enc->height);
  1459. if (enc->sample_aspect_ratio.num) {
  1460. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1461. enc->width * enc->sample_aspect_ratio.num,
  1462. enc->height * enc->sample_aspect_ratio.den,
  1463. 1024 * 1024);
  1464. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1465. " [PAR %d:%d DAR %d:%d]",
  1466. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1467. display_aspect_ratio.num, display_aspect_ratio.den);
  1468. }
  1469. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1470. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1471. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1472. ", %d/%d",
  1473. enc->time_base.num / g, enc->time_base.den / g);
  1474. }
  1475. }
  1476. if (encode) {
  1477. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1478. ", q=%d-%d", enc->qmin, enc->qmax);
  1479. }
  1480. break;
  1481. case AVMEDIA_TYPE_AUDIO:
  1482. snprintf(buf, buf_size,
  1483. "Audio: %s",
  1484. codec_name);
  1485. if (profile)
  1486. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1487. " (%s)", profile);
  1488. if (enc->sample_rate) {
  1489. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1490. ", %d Hz", enc->sample_rate);
  1491. }
  1492. av_strlcat(buf, ", ", buf_size);
  1493. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1494. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1495. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1496. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1497. }
  1498. break;
  1499. case AVMEDIA_TYPE_DATA:
  1500. snprintf(buf, buf_size, "Data: %s", codec_name);
  1501. break;
  1502. case AVMEDIA_TYPE_SUBTITLE:
  1503. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1504. break;
  1505. case AVMEDIA_TYPE_ATTACHMENT:
  1506. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1507. break;
  1508. default:
  1509. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1510. return;
  1511. }
  1512. if (encode) {
  1513. if (enc->flags & CODEC_FLAG_PASS1)
  1514. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1515. ", pass 1");
  1516. if (enc->flags & CODEC_FLAG_PASS2)
  1517. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1518. ", pass 2");
  1519. }
  1520. bitrate = get_bit_rate(enc);
  1521. if (bitrate != 0) {
  1522. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1523. ", %d kb/s", bitrate / 1000);
  1524. }
  1525. }
  1526. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1527. {
  1528. const AVProfile *p;
  1529. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1530. return NULL;
  1531. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1532. if (p->profile == profile)
  1533. return p->name;
  1534. return NULL;
  1535. }
  1536. unsigned avcodec_version(void)
  1537. {
  1538. return LIBAVCODEC_VERSION_INT;
  1539. }
  1540. const char *avcodec_configuration(void)
  1541. {
  1542. return LIBAV_CONFIGURATION;
  1543. }
  1544. const char *avcodec_license(void)
  1545. {
  1546. #define LICENSE_PREFIX "libavcodec license: "
  1547. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1548. }
  1549. void avcodec_flush_buffers(AVCodecContext *avctx)
  1550. {
  1551. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1552. ff_thread_flush(avctx);
  1553. else if (avctx->codec->flush)
  1554. avctx->codec->flush(avctx);
  1555. }
  1556. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1557. {
  1558. switch (codec_id) {
  1559. case AV_CODEC_ID_ADPCM_CT:
  1560. case AV_CODEC_ID_ADPCM_IMA_APC:
  1561. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1562. case AV_CODEC_ID_ADPCM_IMA_WS:
  1563. case AV_CODEC_ID_ADPCM_G722:
  1564. case AV_CODEC_ID_ADPCM_YAMAHA:
  1565. return 4;
  1566. case AV_CODEC_ID_PCM_ALAW:
  1567. case AV_CODEC_ID_PCM_MULAW:
  1568. case AV_CODEC_ID_PCM_S8:
  1569. case AV_CODEC_ID_PCM_U8:
  1570. case AV_CODEC_ID_PCM_ZORK:
  1571. return 8;
  1572. case AV_CODEC_ID_PCM_S16BE:
  1573. case AV_CODEC_ID_PCM_S16LE:
  1574. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1575. case AV_CODEC_ID_PCM_U16BE:
  1576. case AV_CODEC_ID_PCM_U16LE:
  1577. return 16;
  1578. case AV_CODEC_ID_PCM_S24DAUD:
  1579. case AV_CODEC_ID_PCM_S24BE:
  1580. case AV_CODEC_ID_PCM_S24LE:
  1581. case AV_CODEC_ID_PCM_U24BE:
  1582. case AV_CODEC_ID_PCM_U24LE:
  1583. return 24;
  1584. case AV_CODEC_ID_PCM_S32BE:
  1585. case AV_CODEC_ID_PCM_S32LE:
  1586. case AV_CODEC_ID_PCM_U32BE:
  1587. case AV_CODEC_ID_PCM_U32LE:
  1588. case AV_CODEC_ID_PCM_F32BE:
  1589. case AV_CODEC_ID_PCM_F32LE:
  1590. return 32;
  1591. case AV_CODEC_ID_PCM_F64BE:
  1592. case AV_CODEC_ID_PCM_F64LE:
  1593. return 64;
  1594. default:
  1595. return 0;
  1596. }
  1597. }
  1598. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1599. {
  1600. switch (codec_id) {
  1601. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1602. return 2;
  1603. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1604. return 3;
  1605. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1606. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1607. case AV_CODEC_ID_ADPCM_IMA_QT:
  1608. case AV_CODEC_ID_ADPCM_SWF:
  1609. case AV_CODEC_ID_ADPCM_MS:
  1610. return 4;
  1611. default:
  1612. return av_get_exact_bits_per_sample(codec_id);
  1613. }
  1614. }
  1615. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1616. {
  1617. int id, sr, ch, ba, tag, bps;
  1618. id = avctx->codec_id;
  1619. sr = avctx->sample_rate;
  1620. ch = avctx->channels;
  1621. ba = avctx->block_align;
  1622. tag = avctx->codec_tag;
  1623. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1624. /* codecs with an exact constant bits per sample */
  1625. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1626. return (frame_bytes * 8) / (bps * ch);
  1627. bps = avctx->bits_per_coded_sample;
  1628. /* codecs with a fixed packet duration */
  1629. switch (id) {
  1630. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1631. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1632. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1633. case AV_CODEC_ID_AMR_NB:
  1634. case AV_CODEC_ID_GSM:
  1635. case AV_CODEC_ID_QCELP:
  1636. case AV_CODEC_ID_RA_144:
  1637. case AV_CODEC_ID_RA_288: return 160;
  1638. case AV_CODEC_ID_IMC: return 256;
  1639. case AV_CODEC_ID_AMR_WB:
  1640. case AV_CODEC_ID_GSM_MS: return 320;
  1641. case AV_CODEC_ID_MP1: return 384;
  1642. case AV_CODEC_ID_ATRAC1: return 512;
  1643. case AV_CODEC_ID_ATRAC3: return 1024;
  1644. case AV_CODEC_ID_MP2:
  1645. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1646. case AV_CODEC_ID_AC3: return 1536;
  1647. }
  1648. if (sr > 0) {
  1649. /* calc from sample rate */
  1650. if (id == AV_CODEC_ID_TTA)
  1651. return 256 * sr / 245;
  1652. if (ch > 0) {
  1653. /* calc from sample rate and channels */
  1654. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1655. return (480 << (sr / 22050)) / ch;
  1656. }
  1657. }
  1658. if (ba > 0) {
  1659. /* calc from block_align */
  1660. if (id == AV_CODEC_ID_SIPR) {
  1661. switch (ba) {
  1662. case 20: return 160;
  1663. case 19: return 144;
  1664. case 29: return 288;
  1665. case 37: return 480;
  1666. }
  1667. } else if (id == AV_CODEC_ID_ILBC) {
  1668. switch (ba) {
  1669. case 38: return 160;
  1670. case 50: return 240;
  1671. }
  1672. }
  1673. }
  1674. if (frame_bytes > 0) {
  1675. /* calc from frame_bytes only */
  1676. if (id == AV_CODEC_ID_TRUESPEECH)
  1677. return 240 * (frame_bytes / 32);
  1678. if (id == AV_CODEC_ID_NELLYMOSER)
  1679. return 256 * (frame_bytes / 64);
  1680. if (bps > 0) {
  1681. /* calc from frame_bytes and bits_per_coded_sample */
  1682. if (id == AV_CODEC_ID_ADPCM_G726)
  1683. return frame_bytes * 8 / bps;
  1684. }
  1685. if (ch > 0) {
  1686. /* calc from frame_bytes and channels */
  1687. switch (id) {
  1688. case AV_CODEC_ID_ADPCM_4XM:
  1689. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1690. return (frame_bytes - 4 * ch) * 2 / ch;
  1691. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1692. return (frame_bytes - 4) * 2 / ch;
  1693. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1694. return (frame_bytes - 8) * 2 / ch;
  1695. case AV_CODEC_ID_ADPCM_XA:
  1696. return (frame_bytes / 128) * 224 / ch;
  1697. case AV_CODEC_ID_INTERPLAY_DPCM:
  1698. return (frame_bytes - 6 - ch) / ch;
  1699. case AV_CODEC_ID_ROQ_DPCM:
  1700. return (frame_bytes - 8) / ch;
  1701. case AV_CODEC_ID_XAN_DPCM:
  1702. return (frame_bytes - 2 * ch) / ch;
  1703. case AV_CODEC_ID_MACE3:
  1704. return 3 * frame_bytes / ch;
  1705. case AV_CODEC_ID_MACE6:
  1706. return 6 * frame_bytes / ch;
  1707. case AV_CODEC_ID_PCM_LXF:
  1708. return 2 * (frame_bytes / (5 * ch));
  1709. }
  1710. if (tag) {
  1711. /* calc from frame_bytes, channels, and codec_tag */
  1712. if (id == AV_CODEC_ID_SOL_DPCM) {
  1713. if (tag == 3)
  1714. return frame_bytes / ch;
  1715. else
  1716. return frame_bytes * 2 / ch;
  1717. }
  1718. }
  1719. if (ba > 0) {
  1720. /* calc from frame_bytes, channels, and block_align */
  1721. int blocks = frame_bytes / ba;
  1722. switch (avctx->codec_id) {
  1723. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1724. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1725. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1726. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1727. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1728. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1729. case AV_CODEC_ID_ADPCM_MS:
  1730. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1731. }
  1732. }
  1733. if (bps > 0) {
  1734. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1735. switch (avctx->codec_id) {
  1736. case AV_CODEC_ID_PCM_DVD:
  1737. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1738. case AV_CODEC_ID_PCM_BLURAY:
  1739. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1740. case AV_CODEC_ID_S302M:
  1741. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1742. }
  1743. }
  1744. }
  1745. }
  1746. return 0;
  1747. }
  1748. #if !HAVE_THREADS
  1749. int ff_thread_init(AVCodecContext *s)
  1750. {
  1751. return -1;
  1752. }
  1753. #endif
  1754. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1755. {
  1756. unsigned int n = 0;
  1757. while (v >= 0xff) {
  1758. *s++ = 0xff;
  1759. v -= 0xff;
  1760. n++;
  1761. }
  1762. *s = v;
  1763. n++;
  1764. return n;
  1765. }
  1766. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1767. {
  1768. int i;
  1769. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1770. return i;
  1771. }
  1772. #if FF_API_MISSING_SAMPLE
  1773. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1774. {
  1775. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
  1776. "version to the newest one from Git. If the problem still "
  1777. "occurs, it means that your file has a feature which has not "
  1778. "been implemented.\n", feature);
  1779. if(want_sample)
  1780. av_log_ask_for_sample(avc, NULL);
  1781. }
  1782. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1783. {
  1784. va_list argument_list;
  1785. va_start(argument_list, msg);
  1786. if (msg)
  1787. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1788. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1789. "of this file to ftp://upload.libav.org/incoming/ "
  1790. "and contact the libav-devel mailing list.\n");
  1791. va_end(argument_list);
  1792. }
  1793. #endif /* FF_API_MISSING_SAMPLE */
  1794. static AVHWAccel *first_hwaccel = NULL;
  1795. void av_register_hwaccel(AVHWAccel *hwaccel)
  1796. {
  1797. AVHWAccel **p = &first_hwaccel;
  1798. while (*p)
  1799. p = &(*p)->next;
  1800. *p = hwaccel;
  1801. hwaccel->next = NULL;
  1802. }
  1803. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1804. {
  1805. return hwaccel ? hwaccel->next : first_hwaccel;
  1806. }
  1807. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  1808. {
  1809. AVHWAccel *hwaccel = NULL;
  1810. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1811. if (hwaccel->id == codec_id
  1812. && hwaccel->pix_fmt == pix_fmt)
  1813. return hwaccel;
  1814. return NULL;
  1815. }
  1816. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1817. {
  1818. if (ff_lockmgr_cb) {
  1819. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1820. return -1;
  1821. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1822. return -1;
  1823. }
  1824. ff_lockmgr_cb = cb;
  1825. if (ff_lockmgr_cb) {
  1826. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1827. return -1;
  1828. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1829. return -1;
  1830. }
  1831. return 0;
  1832. }
  1833. int avpriv_lock_avformat(void)
  1834. {
  1835. if (ff_lockmgr_cb) {
  1836. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1837. return -1;
  1838. }
  1839. return 0;
  1840. }
  1841. int avpriv_unlock_avformat(void)
  1842. {
  1843. if (ff_lockmgr_cb) {
  1844. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1845. return -1;
  1846. }
  1847. return 0;
  1848. }
  1849. unsigned int avpriv_toupper4(unsigned int x)
  1850. {
  1851. return av_toupper(x & 0xFF) +
  1852. (av_toupper((x >> 8) & 0xFF) << 8) +
  1853. (av_toupper((x >> 16) & 0xFF) << 16) +
  1854. (av_toupper((x >> 24) & 0xFF) << 24);
  1855. }
  1856. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  1857. {
  1858. int ret;
  1859. dst->owner = src->owner;
  1860. ret = av_frame_ref(dst->f, src->f);
  1861. if (ret < 0)
  1862. return ret;
  1863. if (src->progress &&
  1864. !(dst->progress = av_buffer_ref(src->progress))) {
  1865. ff_thread_release_buffer(dst->owner, dst);
  1866. return AVERROR(ENOMEM);
  1867. }
  1868. return 0;
  1869. }
  1870. #if !HAVE_THREADS
  1871. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  1872. {
  1873. f->owner = avctx;
  1874. return ff_get_buffer(avctx, f->f, flags);
  1875. }
  1876. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  1877. {
  1878. av_frame_unref(f->f);
  1879. }
  1880. void ff_thread_finish_setup(AVCodecContext *avctx)
  1881. {
  1882. }
  1883. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  1884. {
  1885. }
  1886. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  1887. {
  1888. }
  1889. #endif
  1890. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  1891. {
  1892. if (codec_id <= AV_CODEC_ID_NONE)
  1893. return AVMEDIA_TYPE_UNKNOWN;
  1894. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  1895. return AVMEDIA_TYPE_VIDEO;
  1896. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  1897. return AVMEDIA_TYPE_AUDIO;
  1898. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  1899. return AVMEDIA_TYPE_SUBTITLE;
  1900. return AVMEDIA_TYPE_UNKNOWN;
  1901. }
  1902. int avcodec_is_open(AVCodecContext *s)
  1903. {
  1904. return !!s->internal;
  1905. }
  1906. const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
  1907. const uint8_t *end,
  1908. uint32_t * restrict state)
  1909. {
  1910. int i;
  1911. assert(p <= end);
  1912. if (p >= end)
  1913. return end;
  1914. for (i = 0; i < 3; i++) {
  1915. uint32_t tmp = *state << 8;
  1916. *state = tmp + *(p++);
  1917. if (tmp == 0x100 || p == end)
  1918. return p;
  1919. }
  1920. while (p < end) {
  1921. if (p[-1] > 1 ) p += 3;
  1922. else if (p[-2] ) p += 2;
  1923. else if (p[-3]|(p[-1]-1)) p++;
  1924. else {
  1925. p++;
  1926. break;
  1927. }
  1928. }
  1929. p = FFMIN(p, end) - 4;
  1930. *state = AV_RB32(p);
  1931. return p + 4;
  1932. }