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.

2334 lines
70KB

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