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.

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