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.

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