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.

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