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.

2277 lines
68KB

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