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.

2341 lines
71KB

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