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.

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