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.

2196 lines
66KB

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