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.

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