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.

2238 lines
67KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "config.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/channel_layout.h"
  30. #include "libavutil/crc.h"
  31. #include "libavutil/frame.h"
  32. #include "libavutil/mathematics.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavutil/samplefmt.h"
  36. #include "libavutil/dict.h"
  37. #include "avcodec.h"
  38. #include "dsputil.h"
  39. #include "libavutil/opt.h"
  40. #include "thread.h"
  41. #include "internal.h"
  42. #include "bytestream.h"
  43. #include "version.h"
  44. #include <stdlib.h>
  45. #include <stdarg.h>
  46. #include <limits.h>
  47. #include <float.h>
  48. static int volatile entangled_thread_counter = 0;
  49. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  50. static void *codec_mutex;
  51. static void *avformat_mutex;
  52. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  53. {
  54. if (min_size < *size)
  55. return ptr;
  56. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  57. ptr = av_realloc(ptr, min_size);
  58. /* we could set this to the unmodified min_size but this is safer
  59. * if the user lost the ptr and uses NULL now
  60. */
  61. if (!ptr)
  62. min_size = 0;
  63. *size = min_size;
  64. return ptr;
  65. }
  66. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  67. {
  68. void **p = ptr;
  69. if (min_size < *size)
  70. return;
  71. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  72. av_free(*p);
  73. *p = av_malloc(min_size);
  74. if (!*p)
  75. min_size = 0;
  76. *size = min_size;
  77. }
  78. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  79. {
  80. void **p = ptr;
  81. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  82. av_freep(p);
  83. *size = 0;
  84. return;
  85. }
  86. av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  87. if (*size)
  88. memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  89. }
  90. /* encoder management */
  91. static AVCodec *first_avcodec = NULL;
  92. AVCodec *av_codec_next(const AVCodec *c)
  93. {
  94. if (c)
  95. return c->next;
  96. else
  97. return first_avcodec;
  98. }
  99. static void avcodec_init(void)
  100. {
  101. static int initialized = 0;
  102. if (initialized != 0)
  103. return;
  104. initialized = 1;
  105. if (CONFIG_DSPUTIL)
  106. ff_dsputil_static_init();
  107. }
  108. int av_codec_is_encoder(const AVCodec *codec)
  109. {
  110. return codec && (codec->encode_sub || codec->encode2);
  111. }
  112. int av_codec_is_decoder(const AVCodec *codec)
  113. {
  114. return codec && codec->decode;
  115. }
  116. void avcodec_register(AVCodec *codec)
  117. {
  118. AVCodec **p;
  119. avcodec_init();
  120. p = &first_avcodec;
  121. while (*p != NULL)
  122. p = &(*p)->next;
  123. *p = codec;
  124. codec->next = NULL;
  125. if (codec->init_static_data)
  126. codec->init_static_data(codec);
  127. }
  128. unsigned avcodec_get_edge_width(void)
  129. {
  130. return EDGE_WIDTH;
  131. }
  132. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  133. {
  134. s->coded_width = width;
  135. s->coded_height = height;
  136. s->width = width;
  137. s->height = height;
  138. }
  139. #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
  140. # define STRIDE_ALIGN 16
  141. #else
  142. # define STRIDE_ALIGN 8
  143. #endif
  144. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  145. int linesize_align[AV_NUM_DATA_POINTERS])
  146. {
  147. int i;
  148. int w_align = 1;
  149. int h_align = 1;
  150. switch (s->pix_fmt) {
  151. case AV_PIX_FMT_YUV420P:
  152. case AV_PIX_FMT_YUYV422:
  153. case AV_PIX_FMT_UYVY422:
  154. case AV_PIX_FMT_YUV422P:
  155. case AV_PIX_FMT_YUV440P:
  156. case AV_PIX_FMT_YUV444P:
  157. case AV_PIX_FMT_GBRP:
  158. case AV_PIX_FMT_GRAY8:
  159. case AV_PIX_FMT_GRAY16BE:
  160. case AV_PIX_FMT_GRAY16LE:
  161. case AV_PIX_FMT_YUVJ420P:
  162. case AV_PIX_FMT_YUVJ422P:
  163. case AV_PIX_FMT_YUVJ440P:
  164. case AV_PIX_FMT_YUVJ444P:
  165. case AV_PIX_FMT_YUVA420P:
  166. case AV_PIX_FMT_YUVA422P:
  167. case AV_PIX_FMT_YUVA444P:
  168. case AV_PIX_FMT_YUV420P9LE:
  169. case AV_PIX_FMT_YUV420P9BE:
  170. case AV_PIX_FMT_YUV420P10LE:
  171. case AV_PIX_FMT_YUV420P10BE:
  172. case AV_PIX_FMT_YUV422P9LE:
  173. case AV_PIX_FMT_YUV422P9BE:
  174. case AV_PIX_FMT_YUV422P10LE:
  175. case AV_PIX_FMT_YUV422P10BE:
  176. case AV_PIX_FMT_YUV444P9LE:
  177. case AV_PIX_FMT_YUV444P9BE:
  178. case AV_PIX_FMT_YUV444P10LE:
  179. case AV_PIX_FMT_YUV444P10BE:
  180. case AV_PIX_FMT_GBRP9LE:
  181. case AV_PIX_FMT_GBRP9BE:
  182. case AV_PIX_FMT_GBRP10LE:
  183. case AV_PIX_FMT_GBRP10BE:
  184. w_align = 16; //FIXME assume 16 pixel per macroblock
  185. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  186. break;
  187. case AV_PIX_FMT_YUV411P:
  188. case AV_PIX_FMT_UYYVYY411:
  189. w_align = 32;
  190. h_align = 8;
  191. break;
  192. case AV_PIX_FMT_YUV410P:
  193. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  194. w_align = 64;
  195. h_align = 64;
  196. }
  197. case AV_PIX_FMT_RGB555:
  198. if (s->codec_id == AV_CODEC_ID_RPZA) {
  199. w_align = 4;
  200. h_align = 4;
  201. }
  202. case AV_PIX_FMT_PAL8:
  203. case AV_PIX_FMT_BGR8:
  204. case AV_PIX_FMT_RGB8:
  205. if (s->codec_id == AV_CODEC_ID_SMC) {
  206. w_align = 4;
  207. h_align = 4;
  208. }
  209. break;
  210. case AV_PIX_FMT_BGR24:
  211. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  212. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  213. w_align = 4;
  214. h_align = 4;
  215. }
  216. break;
  217. default:
  218. w_align = 1;
  219. h_align = 1;
  220. break;
  221. }
  222. *width = FFALIGN(*width, w_align);
  223. *height = FFALIGN(*height, h_align);
  224. if (s->codec_id == AV_CODEC_ID_H264)
  225. // some of the optimized chroma MC reads one line too much
  226. *height += 2;
  227. for (i = 0; i < 4; i++)
  228. linesize_align[i] = STRIDE_ALIGN;
  229. }
  230. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  231. {
  232. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  233. int chroma_shift = desc->log2_chroma_w;
  234. int linesize_align[AV_NUM_DATA_POINTERS];
  235. int align;
  236. avcodec_align_dimensions2(s, width, height, linesize_align);
  237. align = FFMAX(linesize_align[0], linesize_align[3]);
  238. linesize_align[1] <<= chroma_shift;
  239. linesize_align[2] <<= chroma_shift;
  240. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  241. *width = FFALIGN(*width, align);
  242. }
  243. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  244. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  245. int buf_size, int align)
  246. {
  247. int ch, planar, needed_size, ret = 0;
  248. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  249. frame->nb_samples, sample_fmt,
  250. align);
  251. if (buf_size < needed_size)
  252. return AVERROR(EINVAL);
  253. planar = av_sample_fmt_is_planar(sample_fmt);
  254. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  255. if (!(frame->extended_data = av_mallocz(nb_channels *
  256. sizeof(*frame->extended_data))))
  257. return AVERROR(ENOMEM);
  258. } else {
  259. frame->extended_data = frame->data;
  260. }
  261. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  262. buf, nb_channels, frame->nb_samples,
  263. sample_fmt, align)) < 0) {
  264. if (frame->extended_data != frame->data)
  265. av_free(frame->extended_data);
  266. return ret;
  267. }
  268. if (frame->extended_data != frame->data) {
  269. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  270. frame->data[ch] = frame->extended_data[ch];
  271. }
  272. return ret;
  273. }
  274. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  275. {
  276. FramePool *pool = avctx->internal->pool;
  277. int i, ret;
  278. switch (avctx->codec_type) {
  279. case AVMEDIA_TYPE_VIDEO: {
  280. AVPicture picture;
  281. int size[4] = { 0 };
  282. int w = frame->width;
  283. int h = frame->height;
  284. int tmpsize, unaligned;
  285. if (pool->format == frame->format &&
  286. pool->width == frame->width && pool->height == frame->height)
  287. return 0;
  288. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  289. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  290. w += EDGE_WIDTH * 2;
  291. h += EDGE_WIDTH * 2;
  292. }
  293. do {
  294. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  295. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  296. av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
  297. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  298. w += w & ~(w - 1);
  299. unaligned = 0;
  300. for (i = 0; i < 4; i++)
  301. unaligned |= picture.linesize[i] % pool->stride_align[i];
  302. } while (unaligned);
  303. tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
  304. NULL, picture.linesize);
  305. if (tmpsize < 0)
  306. return -1;
  307. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  308. size[i] = picture.data[i + 1] - picture.data[i];
  309. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  310. for (i = 0; i < 4; i++) {
  311. av_buffer_pool_uninit(&pool->pools[i]);
  312. pool->linesize[i] = picture.linesize[i];
  313. if (size[i]) {
  314. pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
  315. if (!pool->pools[i]) {
  316. ret = AVERROR(ENOMEM);
  317. goto fail;
  318. }
  319. }
  320. }
  321. pool->format = frame->format;
  322. pool->width = frame->width;
  323. pool->height = frame->height;
  324. break;
  325. }
  326. case AVMEDIA_TYPE_AUDIO: {
  327. int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
  328. int planar = av_sample_fmt_is_planar(frame->format);
  329. int planes = planar ? ch : 1;
  330. if (pool->format == frame->format && pool->planes == planes &&
  331. pool->channels == ch && frame->nb_samples == pool->samples)
  332. return 0;
  333. av_buffer_pool_uninit(&pool->pools[0]);
  334. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  335. frame->nb_samples, frame->format, 0);
  336. if (ret < 0)
  337. goto fail;
  338. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  339. if (!pool->pools[0]) {
  340. ret = AVERROR(ENOMEM);
  341. goto fail;
  342. }
  343. pool->format = frame->format;
  344. pool->planes = planes;
  345. pool->channels = ch;
  346. pool->samples = frame->nb_samples;
  347. break;
  348. }
  349. default: av_assert0(0);
  350. }
  351. return 0;
  352. fail:
  353. for (i = 0; i < 4; i++)
  354. av_buffer_pool_uninit(&pool->pools[i]);
  355. pool->format = -1;
  356. pool->planes = pool->channels = pool->samples = 0;
  357. pool->width = pool->height = 0;
  358. return ret;
  359. }
  360. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  361. {
  362. FramePool *pool = avctx->internal->pool;
  363. int planes = pool->planes;
  364. int i;
  365. frame->linesize[0] = pool->linesize[0];
  366. if (planes > AV_NUM_DATA_POINTERS) {
  367. frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  368. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  369. frame->extended_buf = av_mallocz(frame->nb_extended_buf *
  370. sizeof(*frame->extended_buf));
  371. if (!frame->extended_data || !frame->extended_buf) {
  372. av_freep(&frame->extended_data);
  373. av_freep(&frame->extended_buf);
  374. return AVERROR(ENOMEM);
  375. }
  376. } else
  377. frame->extended_data = frame->data;
  378. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  379. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  380. if (!frame->buf[i])
  381. goto fail;
  382. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  383. }
  384. for (i = 0; i < frame->nb_extended_buf; i++) {
  385. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  386. if (!frame->extended_buf[i])
  387. goto fail;
  388. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  389. }
  390. if (avctx->debug & FF_DEBUG_BUFFERS)
  391. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  392. return 0;
  393. fail:
  394. av_frame_unref(frame);
  395. return AVERROR(ENOMEM);
  396. }
  397. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  398. {
  399. FramePool *pool = s->internal->pool;
  400. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
  401. int pixel_size = desc->comp[0].step_minus1 + 1;
  402. int h_chroma_shift, v_chroma_shift;
  403. int i;
  404. if (pic->data[0] != NULL) {
  405. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  406. return -1;
  407. }
  408. memset(pic->data, 0, sizeof(pic->data));
  409. pic->extended_data = pic->data;
  410. av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  411. for (i = 0; i < 4 && pool->pools[i]; i++) {
  412. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  413. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  414. pic->linesize[i] = pool->linesize[i];
  415. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  416. if (!pic->buf[i])
  417. goto fail;
  418. // no edge if EDGE EMU or not planar YUV
  419. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
  420. pic->data[i] = pic->buf[i]->data;
  421. else {
  422. pic->data[i] = pic->buf[i]->data +
  423. FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
  424. (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
  425. }
  426. }
  427. for (; i < AV_NUM_DATA_POINTERS; i++) {
  428. pic->data[i] = NULL;
  429. pic->linesize[i] = 0;
  430. }
  431. if (pic->data[1] && !pic->data[2])
  432. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  433. if (s->debug & FF_DEBUG_BUFFERS)
  434. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  435. return 0;
  436. fail:
  437. av_frame_unref(pic);
  438. return AVERROR(ENOMEM);
  439. }
  440. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  441. {
  442. int ret;
  443. if ((ret = update_frame_pool(avctx, frame)) < 0)
  444. return ret;
  445. #if FF_API_GET_BUFFER
  446. frame->type = FF_BUFFER_TYPE_INTERNAL;
  447. #endif
  448. switch (avctx->codec_type) {
  449. case AVMEDIA_TYPE_VIDEO:
  450. return video_get_buffer(avctx, frame);
  451. case AVMEDIA_TYPE_AUDIO:
  452. return audio_get_buffer(avctx, frame);
  453. default:
  454. return -1;
  455. }
  456. }
  457. #if FF_API_GET_BUFFER
  458. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  459. {
  460. return avcodec_default_get_buffer2(avctx, frame, 0);
  461. }
  462. typedef struct CompatReleaseBufPriv {
  463. AVCodecContext avctx;
  464. AVFrame frame;
  465. } CompatReleaseBufPriv;
  466. static void compat_free_buffer(void *opaque, uint8_t *data)
  467. {
  468. CompatReleaseBufPriv *priv = opaque;
  469. if (priv->avctx.release_buffer)
  470. priv->avctx.release_buffer(&priv->avctx, &priv->frame);
  471. av_freep(&priv);
  472. }
  473. static void compat_release_buffer(void *opaque, uint8_t *data)
  474. {
  475. AVBufferRef *buf = opaque;
  476. av_buffer_unref(&buf);
  477. }
  478. #endif
  479. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  480. {
  481. int ret;
  482. switch (avctx->codec_type) {
  483. case AVMEDIA_TYPE_VIDEO:
  484. if (!frame->width)
  485. frame->width = avctx->width;
  486. if (!frame->height)
  487. frame->height = avctx->height;
  488. if (frame->format < 0)
  489. frame->format = avctx->pix_fmt;
  490. if (!frame->sample_aspect_ratio.num)
  491. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  492. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  493. return ret;
  494. break;
  495. case AVMEDIA_TYPE_AUDIO:
  496. if (!frame->sample_rate)
  497. frame->sample_rate = avctx->sample_rate;
  498. if (frame->format < 0)
  499. frame->format = avctx->sample_fmt;
  500. if (!frame->channel_layout) {
  501. if (avctx->channel_layout) {
  502. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  503. avctx->channels) {
  504. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  505. "configuration.\n");
  506. return AVERROR(EINVAL);
  507. }
  508. frame->channel_layout = avctx->channel_layout;
  509. } else {
  510. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  511. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  512. avctx->channels);
  513. return AVERROR(ENOSYS);
  514. }
  515. frame->channel_layout = av_get_default_channel_layout(avctx->channels);
  516. if (!frame->channel_layout)
  517. frame->channel_layout = (1ULL << avctx->channels) - 1;
  518. }
  519. }
  520. break;
  521. default: return AVERROR(EINVAL);
  522. }
  523. frame->pkt_pts = avctx->pkt ? avctx->pkt->pts : AV_NOPTS_VALUE;
  524. frame->reordered_opaque = avctx->reordered_opaque;
  525. #if FF_API_GET_BUFFER
  526. /*
  527. * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
  528. * We wrap each plane in its own AVBuffer. Each of those has a reference to
  529. * a dummy AVBuffer as its private data, unreffing it on free.
  530. * When all the planes are freed, the dummy buffer's free callback calls
  531. * release_buffer().
  532. */
  533. if (avctx->get_buffer) {
  534. CompatReleaseBufPriv *priv = NULL;
  535. AVBufferRef *dummy_buf = NULL;
  536. int planes, i, ret;
  537. if (flags & AV_GET_BUFFER_FLAG_REF)
  538. frame->reference = 1;
  539. ret = avctx->get_buffer(avctx, frame);
  540. if (ret < 0)
  541. return ret;
  542. /* return if the buffers are already set up
  543. * this would happen e.g. when a custom get_buffer() calls
  544. * avcodec_default_get_buffer
  545. */
  546. if (frame->buf[0])
  547. return 0;
  548. priv = av_mallocz(sizeof(*priv));
  549. if (!priv) {
  550. ret = AVERROR(ENOMEM);
  551. goto fail;
  552. }
  553. priv->avctx = *avctx;
  554. priv->frame = *frame;
  555. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
  556. if (!dummy_buf) {
  557. ret = AVERROR(ENOMEM);
  558. goto fail;
  559. }
  560. #define WRAP_PLANE(ref_out, data, data_size) \
  561. do { \
  562. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  563. if (!dummy_ref) { \
  564. ret = AVERROR(ENOMEM); \
  565. goto fail; \
  566. } \
  567. ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
  568. dummy_ref, 0); \
  569. if (!ref_out) { \
  570. av_frame_unref(frame); \
  571. ret = AVERROR(ENOMEM); \
  572. goto fail; \
  573. } \
  574. } while (0)
  575. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  576. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  577. planes = av_pix_fmt_count_planes(frame->format);
  578. /* workaround for AVHWAccel plane count of 0, buf[0] is used as
  579. check for allocated buffers: make libavcodec happy */
  580. if (desc && desc->flags & PIX_FMT_HWACCEL)
  581. planes = 1;
  582. if (!desc || planes <= 0) {
  583. ret = AVERROR(EINVAL);
  584. goto fail;
  585. }
  586. for (i = 0; i < planes; i++) {
  587. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  588. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  589. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  590. }
  591. } else {
  592. int planar = av_sample_fmt_is_planar(frame->format);
  593. planes = planar ? avctx->channels : 1;
  594. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  595. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  596. frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
  597. frame->nb_extended_buf);
  598. if (!frame->extended_buf) {
  599. ret = AVERROR(ENOMEM);
  600. goto fail;
  601. }
  602. }
  603. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  604. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  605. for (i = 0; i < frame->nb_extended_buf; i++)
  606. WRAP_PLANE(frame->extended_buf[i],
  607. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  608. frame->linesize[0]);
  609. }
  610. av_buffer_unref(&dummy_buf);
  611. return 0;
  612. fail:
  613. avctx->release_buffer(avctx, frame);
  614. av_freep(&priv);
  615. av_buffer_unref(&dummy_buf);
  616. return ret;
  617. }
  618. #endif
  619. return avctx->get_buffer2(avctx, frame, flags);
  620. }
  621. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  622. {
  623. AVFrame tmp;
  624. int ret;
  625. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  626. if (!frame->data[0])
  627. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  628. if (av_frame_is_writable(frame))
  629. return 0;
  630. av_frame_move_ref(&tmp, frame);
  631. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  632. if (ret < 0) {
  633. av_frame_unref(&tmp);
  634. return ret;
  635. }
  636. av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
  637. frame->format, frame->width, frame->height);
  638. av_frame_unref(&tmp);
  639. return 0;
  640. }
  641. #if FF_API_GET_BUFFER
  642. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  643. {
  644. av_frame_unref(pic);
  645. }
  646. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  647. {
  648. av_assert0(0);
  649. }
  650. #endif
  651. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  652. {
  653. int i;
  654. for (i = 0; i < count; i++) {
  655. int r = func(c, (char *)arg + i * size);
  656. if (ret)
  657. ret[i] = r;
  658. }
  659. return 0;
  660. }
  661. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  662. {
  663. int i;
  664. for (i = 0; i < count; i++) {
  665. int r = func(c, arg, i, 0);
  666. if (ret)
  667. ret[i] = r;
  668. }
  669. return 0;
  670. }
  671. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  672. {
  673. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  674. return desc->flags & PIX_FMT_HWACCEL;
  675. }
  676. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  677. {
  678. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  679. ++fmt;
  680. return fmt[0];
  681. }
  682. void avcodec_get_frame_defaults(AVFrame *frame)
  683. {
  684. if (frame->extended_data != frame->data)
  685. av_freep(&frame->extended_data);
  686. memset(frame, 0, sizeof(AVFrame));
  687. frame->pts = AV_NOPTS_VALUE;
  688. frame->key_frame = 1;
  689. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  690. frame->format = -1; /* unknown */
  691. frame->extended_data = frame->data;
  692. }
  693. AVFrame *avcodec_alloc_frame(void)
  694. {
  695. AVFrame *frame = av_mallocz(sizeof(AVFrame));
  696. if (frame == NULL)
  697. return NULL;
  698. avcodec_get_frame_defaults(frame);
  699. return frame;
  700. }
  701. void avcodec_free_frame(AVFrame **frame)
  702. {
  703. AVFrame *f;
  704. if (!frame || !*frame)
  705. return;
  706. f = *frame;
  707. if (f->extended_data != f->data)
  708. av_freep(&f->extended_data);
  709. av_freep(frame);
  710. }
  711. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  712. {
  713. int ret = 0;
  714. AVDictionary *tmp = NULL;
  715. if (avcodec_is_open(avctx))
  716. return 0;
  717. if ((!codec && !avctx->codec)) {
  718. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  719. return AVERROR(EINVAL);
  720. }
  721. if ((codec && avctx->codec && codec != avctx->codec)) {
  722. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  723. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  724. return AVERROR(EINVAL);
  725. }
  726. if (!codec)
  727. codec = avctx->codec;
  728. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  729. return AVERROR(EINVAL);
  730. if (options)
  731. av_dict_copy(&tmp, *options, 0);
  732. /* If there is a user-supplied mutex locking routine, call it. */
  733. if (ff_lockmgr_cb) {
  734. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  735. return -1;
  736. }
  737. entangled_thread_counter++;
  738. if (entangled_thread_counter != 1) {
  739. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  740. ret = -1;
  741. goto end;
  742. }
  743. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  744. if (!avctx->internal) {
  745. ret = AVERROR(ENOMEM);
  746. goto end;
  747. }
  748. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  749. if (!avctx->internal->pool) {
  750. ret = AVERROR(ENOMEM);
  751. goto free_and_end;
  752. }
  753. if (codec->priv_data_size > 0) {
  754. if (!avctx->priv_data) {
  755. avctx->priv_data = av_mallocz(codec->priv_data_size);
  756. if (!avctx->priv_data) {
  757. ret = AVERROR(ENOMEM);
  758. goto end;
  759. }
  760. if (codec->priv_class) {
  761. *(const AVClass **)avctx->priv_data = codec->priv_class;
  762. av_opt_set_defaults(avctx->priv_data);
  763. }
  764. }
  765. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  766. goto free_and_end;
  767. } else {
  768. avctx->priv_data = NULL;
  769. }
  770. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  771. goto free_and_end;
  772. if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
  773. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  774. else if (avctx->width && avctx->height)
  775. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  776. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  777. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  778. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  779. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  780. avcodec_set_dimensions(avctx, 0, 0);
  781. }
  782. /* if the decoder init function was already called previously,
  783. * free the already allocated subtitle_header before overwriting it */
  784. if (av_codec_is_decoder(codec))
  785. av_freep(&avctx->subtitle_header);
  786. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  787. ret = AVERROR(EINVAL);
  788. goto free_and_end;
  789. }
  790. avctx->codec = codec;
  791. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  792. avctx->codec_id == AV_CODEC_ID_NONE) {
  793. avctx->codec_type = codec->type;
  794. avctx->codec_id = codec->id;
  795. }
  796. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  797. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  798. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  799. ret = AVERROR(EINVAL);
  800. goto free_and_end;
  801. }
  802. avctx->frame_number = 0;
  803. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  804. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  805. ret = AVERROR_EXPERIMENTAL;
  806. goto free_and_end;
  807. }
  808. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  809. (!avctx->time_base.num || !avctx->time_base.den)) {
  810. avctx->time_base.num = 1;
  811. avctx->time_base.den = avctx->sample_rate;
  812. }
  813. if (HAVE_THREADS && !avctx->thread_opaque) {
  814. ret = ff_thread_init(avctx);
  815. if (ret < 0) {
  816. goto free_and_end;
  817. }
  818. }
  819. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  820. avctx->thread_count = 1;
  821. if (av_codec_is_encoder(avctx->codec)) {
  822. int i;
  823. if (avctx->codec->sample_fmts) {
  824. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  825. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  826. break;
  827. if (avctx->channels == 1 &&
  828. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  829. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  830. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  831. break;
  832. }
  833. }
  834. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  835. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  836. ret = AVERROR(EINVAL);
  837. goto free_and_end;
  838. }
  839. }
  840. if (avctx->codec->pix_fmts) {
  841. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  842. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  843. break;
  844. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
  845. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  846. ret = AVERROR(EINVAL);
  847. goto free_and_end;
  848. }
  849. }
  850. if (avctx->codec->supported_samplerates) {
  851. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  852. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  853. break;
  854. if (avctx->codec->supported_samplerates[i] == 0) {
  855. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  856. ret = AVERROR(EINVAL);
  857. goto free_and_end;
  858. }
  859. }
  860. if (avctx->codec->channel_layouts) {
  861. if (!avctx->channel_layout) {
  862. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  863. } else {
  864. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  865. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  866. break;
  867. if (avctx->codec->channel_layouts[i] == 0) {
  868. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  869. ret = AVERROR(EINVAL);
  870. goto free_and_end;
  871. }
  872. }
  873. }
  874. if (avctx->channel_layout && avctx->channels) {
  875. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  876. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  877. ret = AVERROR(EINVAL);
  878. goto free_and_end;
  879. }
  880. } else if (avctx->channel_layout) {
  881. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  882. }
  883. if (!avctx->rc_initial_buffer_occupancy)
  884. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  885. }
  886. if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  887. ret = avctx->codec->init(avctx);
  888. if (ret < 0) {
  889. goto free_and_end;
  890. }
  891. }
  892. if (av_codec_is_decoder(avctx->codec)) {
  893. /* validate channel layout from the decoder */
  894. if (avctx->channel_layout) {
  895. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  896. if (!avctx->channels)
  897. avctx->channels = channels;
  898. else if (channels != avctx->channels) {
  899. av_log(avctx, AV_LOG_WARNING,
  900. "channel layout does not match number of channels\n");
  901. avctx->channel_layout = 0;
  902. }
  903. }
  904. if (avctx->channels && avctx->channels < 0 ||
  905. avctx->channels > FF_SANE_NB_CHANNELS) {
  906. ret = AVERROR(EINVAL);
  907. goto free_and_end;
  908. }
  909. }
  910. end:
  911. entangled_thread_counter--;
  912. /* Release any user-supplied mutex. */
  913. if (ff_lockmgr_cb) {
  914. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  915. }
  916. if (options) {
  917. av_dict_free(options);
  918. *options = tmp;
  919. }
  920. return ret;
  921. free_and_end:
  922. av_dict_free(&tmp);
  923. av_freep(&avctx->priv_data);
  924. if (avctx->internal)
  925. av_freep(&avctx->internal->pool);
  926. av_freep(&avctx->internal);
  927. avctx->codec = NULL;
  928. goto end;
  929. }
  930. int ff_alloc_packet(AVPacket *avpkt, int size)
  931. {
  932. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  933. return AVERROR(EINVAL);
  934. if (avpkt->data) {
  935. AVBufferRef *buf = avpkt->buf;
  936. #if FF_API_DESTRUCT_PACKET
  937. void *destruct = avpkt->destruct;
  938. #endif
  939. if (avpkt->size < size)
  940. return AVERROR(EINVAL);
  941. av_init_packet(avpkt);
  942. #if FF_API_DESTRUCT_PACKET
  943. avpkt->destruct = destruct;
  944. #endif
  945. avpkt->buf = buf;
  946. avpkt->size = size;
  947. return 0;
  948. } else {
  949. return av_new_packet(avpkt, size);
  950. }
  951. }
  952. /**
  953. * Pad last frame with silence.
  954. */
  955. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  956. {
  957. AVFrame *frame = NULL;
  958. uint8_t *buf = NULL;
  959. int ret;
  960. if (!(frame = avcodec_alloc_frame()))
  961. return AVERROR(ENOMEM);
  962. *frame = *src;
  963. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  964. s->frame_size, s->sample_fmt, 0)) < 0)
  965. goto fail;
  966. if (!(buf = av_malloc(ret))) {
  967. ret = AVERROR(ENOMEM);
  968. goto fail;
  969. }
  970. frame->nb_samples = s->frame_size;
  971. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  972. buf, ret, 0)) < 0)
  973. goto fail;
  974. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  975. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  976. goto fail;
  977. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  978. frame->nb_samples - src->nb_samples,
  979. s->channels, s->sample_fmt)) < 0)
  980. goto fail;
  981. *dst = frame;
  982. return 0;
  983. fail:
  984. if (frame->extended_data != frame->data)
  985. av_freep(&frame->extended_data);
  986. av_freep(&buf);
  987. av_freep(&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. if (padded_frame) {
  1071. av_freep(&padded_frame->data[0]);
  1072. if (padded_frame->extended_data != padded_frame->data)
  1073. av_freep(&padded_frame->extended_data);
  1074. av_freep(&padded_frame);
  1075. }
  1076. return ret;
  1077. }
  1078. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1079. AVPacket *avpkt,
  1080. const AVFrame *frame,
  1081. int *got_packet_ptr)
  1082. {
  1083. int ret;
  1084. int user_packet = !!avpkt->data;
  1085. *got_packet_ptr = 0;
  1086. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1087. av_free_packet(avpkt);
  1088. av_init_packet(avpkt);
  1089. avpkt->size = 0;
  1090. return 0;
  1091. }
  1092. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1093. return AVERROR(EINVAL);
  1094. av_assert0(avctx->codec->encode2);
  1095. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1096. if (!ret) {
  1097. if (!*got_packet_ptr)
  1098. avpkt->size = 0;
  1099. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1100. avpkt->pts = avpkt->dts = frame->pts;
  1101. if (!user_packet && avpkt->size) {
  1102. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  1103. if (ret >= 0)
  1104. avpkt->data = avpkt->buf->data;
  1105. }
  1106. avctx->frame_number++;
  1107. }
  1108. if (ret < 0 || !*got_packet_ptr)
  1109. av_free_packet(avpkt);
  1110. emms_c();
  1111. return ret;
  1112. }
  1113. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1114. const AVSubtitle *sub)
  1115. {
  1116. int ret;
  1117. if (sub->start_display_time) {
  1118. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1119. return -1;
  1120. }
  1121. if (sub->num_rects == 0 || !sub->rects)
  1122. return -1;
  1123. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1124. avctx->frame_number++;
  1125. return ret;
  1126. }
  1127. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1128. {
  1129. int size = 0;
  1130. const uint8_t *data;
  1131. uint32_t flags;
  1132. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1133. return;
  1134. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1135. if (!data || size < 4)
  1136. return;
  1137. flags = bytestream_get_le32(&data);
  1138. size -= 4;
  1139. if (size < 4) /* Required for any of the changes */
  1140. return;
  1141. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1142. avctx->channels = bytestream_get_le32(&data);
  1143. size -= 4;
  1144. }
  1145. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1146. if (size < 8)
  1147. return;
  1148. avctx->channel_layout = bytestream_get_le64(&data);
  1149. size -= 8;
  1150. }
  1151. if (size < 4)
  1152. return;
  1153. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1154. avctx->sample_rate = bytestream_get_le32(&data);
  1155. size -= 4;
  1156. }
  1157. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1158. if (size < 8)
  1159. return;
  1160. avctx->width = bytestream_get_le32(&data);
  1161. avctx->height = bytestream_get_le32(&data);
  1162. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1163. size -= 8;
  1164. }
  1165. }
  1166. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1167. int *got_picture_ptr,
  1168. AVPacket *avpkt)
  1169. {
  1170. AVCodecInternal *avci = avctx->internal;
  1171. int ret;
  1172. *got_picture_ptr = 0;
  1173. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1174. return -1;
  1175. avctx->pkt = avpkt;
  1176. apply_param_change(avctx, avpkt);
  1177. avcodec_get_frame_defaults(picture);
  1178. if (!avctx->refcounted_frames)
  1179. av_frame_unref(&avci->to_free);
  1180. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1181. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1182. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1183. avpkt);
  1184. else {
  1185. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1186. avpkt);
  1187. picture->pkt_dts = avpkt->dts;
  1188. /* get_buffer is supposed to set frame parameters */
  1189. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1190. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1191. picture->width = avctx->width;
  1192. picture->height = avctx->height;
  1193. picture->format = avctx->pix_fmt;
  1194. }
  1195. }
  1196. emms_c(); //needed to avoid an emms_c() call before every return;
  1197. if (ret < 0 && picture->data[0])
  1198. av_frame_unref(picture);
  1199. if (*got_picture_ptr) {
  1200. if (!avctx->refcounted_frames) {
  1201. avci->to_free = *picture;
  1202. avci->to_free.extended_data = avci->to_free.data;
  1203. }
  1204. avctx->frame_number++;
  1205. }
  1206. } else
  1207. ret = 0;
  1208. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1209. * make sure it's set correctly */
  1210. picture->extended_data = picture->data;
  1211. return ret;
  1212. }
  1213. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1214. AVFrame *frame,
  1215. int *got_frame_ptr,
  1216. AVPacket *avpkt)
  1217. {
  1218. AVCodecInternal *avci = avctx->internal;
  1219. int planar, channels;
  1220. int ret = 0;
  1221. *got_frame_ptr = 0;
  1222. avctx->pkt = avpkt;
  1223. if (!avpkt->data && avpkt->size) {
  1224. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1225. return AVERROR(EINVAL);
  1226. }
  1227. apply_param_change(avctx, avpkt);
  1228. avcodec_get_frame_defaults(frame);
  1229. if (!avctx->refcounted_frames)
  1230. av_frame_unref(&avci->to_free);
  1231. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1232. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1233. if (ret >= 0 && *got_frame_ptr) {
  1234. avctx->frame_number++;
  1235. frame->pkt_dts = avpkt->dts;
  1236. if (frame->format == AV_SAMPLE_FMT_NONE)
  1237. frame->format = avctx->sample_fmt;
  1238. if (!avctx->refcounted_frames) {
  1239. avci->to_free = *frame;
  1240. avci->to_free.extended_data = avci->to_free.data;
  1241. }
  1242. }
  1243. if (ret < 0 && frame->data[0])
  1244. av_frame_unref(frame);
  1245. }
  1246. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1247. * make sure it's set correctly; assume decoders that actually use
  1248. * extended_data are doing it correctly */
  1249. planar = av_sample_fmt_is_planar(frame->format);
  1250. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1251. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1252. frame->extended_data = frame->data;
  1253. return ret;
  1254. }
  1255. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1256. int *got_sub_ptr,
  1257. AVPacket *avpkt)
  1258. {
  1259. int ret;
  1260. avctx->pkt = avpkt;
  1261. *got_sub_ptr = 0;
  1262. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1263. if (*got_sub_ptr)
  1264. avctx->frame_number++;
  1265. return ret;
  1266. }
  1267. void avsubtitle_free(AVSubtitle *sub)
  1268. {
  1269. int i;
  1270. for (i = 0; i < sub->num_rects; i++) {
  1271. av_freep(&sub->rects[i]->pict.data[0]);
  1272. av_freep(&sub->rects[i]->pict.data[1]);
  1273. av_freep(&sub->rects[i]->pict.data[2]);
  1274. av_freep(&sub->rects[i]->pict.data[3]);
  1275. av_freep(&sub->rects[i]->text);
  1276. av_freep(&sub->rects[i]->ass);
  1277. av_freep(&sub->rects[i]);
  1278. }
  1279. av_freep(&sub->rects);
  1280. memset(sub, 0, sizeof(AVSubtitle));
  1281. }
  1282. av_cold int avcodec_close(AVCodecContext *avctx)
  1283. {
  1284. /* If there is a user-supplied mutex locking routine, call it. */
  1285. if (ff_lockmgr_cb) {
  1286. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1287. return -1;
  1288. }
  1289. entangled_thread_counter++;
  1290. if (entangled_thread_counter != 1) {
  1291. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1292. entangled_thread_counter--;
  1293. return -1;
  1294. }
  1295. if (avcodec_is_open(avctx)) {
  1296. FramePool *pool = avctx->internal->pool;
  1297. int i;
  1298. if (HAVE_THREADS && avctx->thread_opaque)
  1299. ff_thread_free(avctx);
  1300. if (avctx->codec && avctx->codec->close)
  1301. avctx->codec->close(avctx);
  1302. avctx->coded_frame = NULL;
  1303. if (!avctx->refcounted_frames)
  1304. av_frame_unref(&avctx->internal->to_free);
  1305. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  1306. av_buffer_pool_uninit(&pool->pools[i]);
  1307. av_freep(&avctx->internal->pool);
  1308. av_freep(&avctx->internal);
  1309. }
  1310. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1311. av_opt_free(avctx->priv_data);
  1312. av_opt_free(avctx);
  1313. av_freep(&avctx->priv_data);
  1314. if (av_codec_is_encoder(avctx->codec))
  1315. av_freep(&avctx->extradata);
  1316. avctx->codec = NULL;
  1317. avctx->active_thread_type = 0;
  1318. entangled_thread_counter--;
  1319. /* Release any user-supplied mutex. */
  1320. if (ff_lockmgr_cb) {
  1321. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1322. }
  1323. return 0;
  1324. }
  1325. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1326. {
  1327. AVCodec *p, *experimental = NULL;
  1328. p = first_avcodec;
  1329. while (p) {
  1330. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1331. p->id == id) {
  1332. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1333. experimental = p;
  1334. } else
  1335. return p;
  1336. }
  1337. p = p->next;
  1338. }
  1339. return experimental;
  1340. }
  1341. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1342. {
  1343. return find_encdec(id, 1);
  1344. }
  1345. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1346. {
  1347. AVCodec *p;
  1348. if (!name)
  1349. return NULL;
  1350. p = first_avcodec;
  1351. while (p) {
  1352. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1353. return p;
  1354. p = p->next;
  1355. }
  1356. return NULL;
  1357. }
  1358. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1359. {
  1360. return find_encdec(id, 0);
  1361. }
  1362. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1363. {
  1364. AVCodec *p;
  1365. if (!name)
  1366. return NULL;
  1367. p = first_avcodec;
  1368. while (p) {
  1369. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1370. return p;
  1371. p = p->next;
  1372. }
  1373. return NULL;
  1374. }
  1375. static int get_bit_rate(AVCodecContext *ctx)
  1376. {
  1377. int bit_rate;
  1378. int bits_per_sample;
  1379. switch (ctx->codec_type) {
  1380. case AVMEDIA_TYPE_VIDEO:
  1381. case AVMEDIA_TYPE_DATA:
  1382. case AVMEDIA_TYPE_SUBTITLE:
  1383. case AVMEDIA_TYPE_ATTACHMENT:
  1384. bit_rate = ctx->bit_rate;
  1385. break;
  1386. case AVMEDIA_TYPE_AUDIO:
  1387. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1388. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1389. break;
  1390. default:
  1391. bit_rate = 0;
  1392. break;
  1393. }
  1394. return bit_rate;
  1395. }
  1396. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1397. {
  1398. int i, len, ret = 0;
  1399. #define TAG_PRINT(x) \
  1400. (((x) >= '0' && (x) <= '9') || \
  1401. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1402. ((x) == '.' || (x) == ' '))
  1403. for (i = 0; i < 4; i++) {
  1404. len = snprintf(buf, buf_size,
  1405. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1406. buf += len;
  1407. buf_size = buf_size > len ? buf_size - len : 0;
  1408. ret += len;
  1409. codec_tag >>= 8;
  1410. }
  1411. return ret;
  1412. }
  1413. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1414. {
  1415. const char *codec_name;
  1416. const char *profile = NULL;
  1417. const AVCodec *p;
  1418. char buf1[32];
  1419. int bitrate;
  1420. AVRational display_aspect_ratio;
  1421. if (enc->codec)
  1422. p = enc->codec;
  1423. else if (encode)
  1424. p = avcodec_find_encoder(enc->codec_id);
  1425. else
  1426. p = avcodec_find_decoder(enc->codec_id);
  1427. if (p) {
  1428. codec_name = p->name;
  1429. profile = av_get_profile_name(p, enc->profile);
  1430. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1431. /* fake mpeg2 transport stream codec (currently not
  1432. * registered) */
  1433. codec_name = "mpeg2ts";
  1434. } else if (enc->codec_name[0] != '\0') {
  1435. codec_name = enc->codec_name;
  1436. } else {
  1437. /* output avi tags */
  1438. char tag_buf[32];
  1439. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1440. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1441. codec_name = buf1;
  1442. }
  1443. switch (enc->codec_type) {
  1444. case AVMEDIA_TYPE_VIDEO:
  1445. snprintf(buf, buf_size,
  1446. "Video: %s%s",
  1447. codec_name, enc->mb_decision ? " (hq)" : "");
  1448. if (profile)
  1449. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1450. " (%s)", profile);
  1451. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1452. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1453. ", %s",
  1454. av_get_pix_fmt_name(enc->pix_fmt));
  1455. }
  1456. if (enc->width) {
  1457. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1458. ", %dx%d",
  1459. enc->width, enc->height);
  1460. if (enc->sample_aspect_ratio.num) {
  1461. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1462. enc->width * enc->sample_aspect_ratio.num,
  1463. enc->height * enc->sample_aspect_ratio.den,
  1464. 1024 * 1024);
  1465. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1466. " [PAR %d:%d DAR %d:%d]",
  1467. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1468. display_aspect_ratio.num, display_aspect_ratio.den);
  1469. }
  1470. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1471. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1472. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1473. ", %d/%d",
  1474. enc->time_base.num / g, enc->time_base.den / g);
  1475. }
  1476. }
  1477. if (encode) {
  1478. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1479. ", q=%d-%d", enc->qmin, enc->qmax);
  1480. }
  1481. break;
  1482. case AVMEDIA_TYPE_AUDIO:
  1483. snprintf(buf, buf_size,
  1484. "Audio: %s",
  1485. codec_name);
  1486. if (profile)
  1487. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1488. " (%s)", profile);
  1489. if (enc->sample_rate) {
  1490. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1491. ", %d Hz", enc->sample_rate);
  1492. }
  1493. av_strlcat(buf, ", ", buf_size);
  1494. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1495. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1496. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1497. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1498. }
  1499. break;
  1500. case AVMEDIA_TYPE_DATA:
  1501. snprintf(buf, buf_size, "Data: %s", codec_name);
  1502. break;
  1503. case AVMEDIA_TYPE_SUBTITLE:
  1504. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1505. break;
  1506. case AVMEDIA_TYPE_ATTACHMENT:
  1507. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1508. break;
  1509. default:
  1510. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1511. return;
  1512. }
  1513. if (encode) {
  1514. if (enc->flags & CODEC_FLAG_PASS1)
  1515. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1516. ", pass 1");
  1517. if (enc->flags & CODEC_FLAG_PASS2)
  1518. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1519. ", pass 2");
  1520. }
  1521. bitrate = get_bit_rate(enc);
  1522. if (bitrate != 0) {
  1523. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1524. ", %d kb/s", bitrate / 1000);
  1525. }
  1526. }
  1527. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1528. {
  1529. const AVProfile *p;
  1530. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1531. return NULL;
  1532. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1533. if (p->profile == profile)
  1534. return p->name;
  1535. return NULL;
  1536. }
  1537. unsigned avcodec_version(void)
  1538. {
  1539. return LIBAVCODEC_VERSION_INT;
  1540. }
  1541. const char *avcodec_configuration(void)
  1542. {
  1543. return LIBAV_CONFIGURATION;
  1544. }
  1545. const char *avcodec_license(void)
  1546. {
  1547. #define LICENSE_PREFIX "libavcodec license: "
  1548. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1549. }
  1550. void avcodec_flush_buffers(AVCodecContext *avctx)
  1551. {
  1552. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1553. ff_thread_flush(avctx);
  1554. else if (avctx->codec->flush)
  1555. avctx->codec->flush(avctx);
  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 (ff_lockmgr_cb) {
  1820. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1821. return -1;
  1822. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1823. return -1;
  1824. }
  1825. ff_lockmgr_cb = cb;
  1826. if (ff_lockmgr_cb) {
  1827. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1828. return -1;
  1829. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1830. return -1;
  1831. }
  1832. return 0;
  1833. }
  1834. int avpriv_lock_avformat(void)
  1835. {
  1836. if (ff_lockmgr_cb) {
  1837. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1838. return -1;
  1839. }
  1840. return 0;
  1841. }
  1842. int avpriv_unlock_avformat(void)
  1843. {
  1844. if (ff_lockmgr_cb) {
  1845. if ((*ff_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. }