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.

3204 lines
103KB

  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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/bprint.h"
  31. #include "libavutil/channel_layout.h"
  32. #include "libavutil/crc.h"
  33. #include "libavutil/frame.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 "libavutil/avassert.h"
  40. #include "avcodec.h"
  41. #include "dsputil.h"
  42. #include "libavutil/opt.h"
  43. #include "thread.h"
  44. #include "frame_thread_encoder.h"
  45. #include "internal.h"
  46. #include "bytestream.h"
  47. #include "version.h"
  48. #include <stdlib.h>
  49. #include <stdarg.h>
  50. #include <limits.h>
  51. #include <float.h>
  52. #if CONFIG_ICONV
  53. # include <iconv.h>
  54. #endif
  55. volatile int ff_avcodec_locked;
  56. static int volatile entangled_thread_counter = 0;
  57. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
  58. static void *codec_mutex;
  59. static void *avformat_mutex;
  60. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  61. {
  62. if (min_size < *size)
  63. return ptr;
  64. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  65. ptr = av_realloc(ptr, min_size);
  66. /* we could set this to the unmodified min_size but this is safer
  67. * if the user lost the ptr and uses NULL now
  68. */
  69. if (!ptr)
  70. min_size = 0;
  71. *size = min_size;
  72. return ptr;
  73. }
  74. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  75. {
  76. void **p = ptr;
  77. if (min_size < *size)
  78. return 0;
  79. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  80. av_free(*p);
  81. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  82. if (!*p)
  83. min_size = 0;
  84. *size = min_size;
  85. return 1;
  86. }
  87. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  88. {
  89. ff_fast_malloc(ptr, size, min_size, 0);
  90. }
  91. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  92. {
  93. uint8_t **p = ptr;
  94. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  95. av_freep(p);
  96. *size = 0;
  97. return;
  98. }
  99. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  100. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  101. }
  102. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  103. {
  104. uint8_t **p = ptr;
  105. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  106. av_freep(p);
  107. *size = 0;
  108. return;
  109. }
  110. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  111. memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  112. }
  113. /* encoder management */
  114. static AVCodec *first_avcodec = NULL;
  115. AVCodec *av_codec_next(const AVCodec *c)
  116. {
  117. if (c)
  118. return c->next;
  119. else
  120. return first_avcodec;
  121. }
  122. static av_cold void avcodec_init(void)
  123. {
  124. static int initialized = 0;
  125. if (initialized != 0)
  126. return;
  127. initialized = 1;
  128. if (CONFIG_DSPUTIL)
  129. ff_dsputil_static_init();
  130. }
  131. int av_codec_is_encoder(const AVCodec *codec)
  132. {
  133. return codec && (codec->encode_sub || codec->encode2);
  134. }
  135. int av_codec_is_decoder(const AVCodec *codec)
  136. {
  137. return codec && codec->decode;
  138. }
  139. av_cold void avcodec_register(AVCodec *codec)
  140. {
  141. AVCodec **p;
  142. avcodec_init();
  143. p = &first_avcodec;
  144. while (*p != NULL)
  145. p = &(*p)->next;
  146. *p = codec;
  147. codec->next = NULL;
  148. if (codec->init_static_data)
  149. codec->init_static_data(codec);
  150. }
  151. unsigned avcodec_get_edge_width(void)
  152. {
  153. return EDGE_WIDTH;
  154. }
  155. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  156. {
  157. s->coded_width = width;
  158. s->coded_height = height;
  159. s->width = FF_CEIL_RSHIFT(width, s->lowres);
  160. s->height = FF_CEIL_RSHIFT(height, s->lowres);
  161. }
  162. #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
  163. # define STRIDE_ALIGN 16
  164. #else
  165. # define STRIDE_ALIGN 8
  166. #endif
  167. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  168. int linesize_align[AV_NUM_DATA_POINTERS])
  169. {
  170. int i;
  171. int w_align = 1;
  172. int h_align = 1;
  173. switch (s->pix_fmt) {
  174. case AV_PIX_FMT_YUV420P:
  175. case AV_PIX_FMT_YUYV422:
  176. case AV_PIX_FMT_UYVY422:
  177. case AV_PIX_FMT_YUV422P:
  178. case AV_PIX_FMT_YUV440P:
  179. case AV_PIX_FMT_YUV444P:
  180. case AV_PIX_FMT_GBRAP:
  181. case AV_PIX_FMT_GBRP:
  182. case AV_PIX_FMT_GRAY8:
  183. case AV_PIX_FMT_GRAY16BE:
  184. case AV_PIX_FMT_GRAY16LE:
  185. case AV_PIX_FMT_YUVJ420P:
  186. case AV_PIX_FMT_YUVJ422P:
  187. case AV_PIX_FMT_YUVJ440P:
  188. case AV_PIX_FMT_YUVJ444P:
  189. case AV_PIX_FMT_YUVA420P:
  190. case AV_PIX_FMT_YUVA422P:
  191. case AV_PIX_FMT_YUVA444P:
  192. case AV_PIX_FMT_YUV420P9LE:
  193. case AV_PIX_FMT_YUV420P9BE:
  194. case AV_PIX_FMT_YUV420P10LE:
  195. case AV_PIX_FMT_YUV420P10BE:
  196. case AV_PIX_FMT_YUV420P12LE:
  197. case AV_PIX_FMT_YUV420P12BE:
  198. case AV_PIX_FMT_YUV420P14LE:
  199. case AV_PIX_FMT_YUV420P14BE:
  200. case AV_PIX_FMT_YUV422P9LE:
  201. case AV_PIX_FMT_YUV422P9BE:
  202. case AV_PIX_FMT_YUV422P10LE:
  203. case AV_PIX_FMT_YUV422P10BE:
  204. case AV_PIX_FMT_YUV422P12LE:
  205. case AV_PIX_FMT_YUV422P12BE:
  206. case AV_PIX_FMT_YUV422P14LE:
  207. case AV_PIX_FMT_YUV422P14BE:
  208. case AV_PIX_FMT_YUV444P9LE:
  209. case AV_PIX_FMT_YUV444P9BE:
  210. case AV_PIX_FMT_YUV444P10LE:
  211. case AV_PIX_FMT_YUV444P10BE:
  212. case AV_PIX_FMT_YUV444P12LE:
  213. case AV_PIX_FMT_YUV444P12BE:
  214. case AV_PIX_FMT_YUV444P14LE:
  215. case AV_PIX_FMT_YUV444P14BE:
  216. case AV_PIX_FMT_GBRP9LE:
  217. case AV_PIX_FMT_GBRP9BE:
  218. case AV_PIX_FMT_GBRP10LE:
  219. case AV_PIX_FMT_GBRP10BE:
  220. case AV_PIX_FMT_GBRP12LE:
  221. case AV_PIX_FMT_GBRP12BE:
  222. case AV_PIX_FMT_GBRP14LE:
  223. case AV_PIX_FMT_GBRP14BE:
  224. w_align = 16; //FIXME assume 16 pixel per macroblock
  225. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  226. break;
  227. case AV_PIX_FMT_YUV411P:
  228. case AV_PIX_FMT_UYYVYY411:
  229. w_align = 32;
  230. h_align = 8;
  231. break;
  232. case AV_PIX_FMT_YUV410P:
  233. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  234. w_align = 64;
  235. h_align = 64;
  236. }
  237. break;
  238. case AV_PIX_FMT_RGB555:
  239. if (s->codec_id == AV_CODEC_ID_RPZA) {
  240. w_align = 4;
  241. h_align = 4;
  242. }
  243. break;
  244. case AV_PIX_FMT_PAL8:
  245. case AV_PIX_FMT_BGR8:
  246. case AV_PIX_FMT_RGB8:
  247. if (s->codec_id == AV_CODEC_ID_SMC ||
  248. s->codec_id == AV_CODEC_ID_CINEPAK) {
  249. w_align = 4;
  250. h_align = 4;
  251. }
  252. break;
  253. case AV_PIX_FMT_BGR24:
  254. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  255. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  256. w_align = 4;
  257. h_align = 4;
  258. }
  259. break;
  260. case AV_PIX_FMT_RGB24:
  261. if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  262. w_align = 4;
  263. h_align = 4;
  264. }
  265. break;
  266. default:
  267. w_align = 1;
  268. h_align = 1;
  269. break;
  270. }
  271. if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
  272. w_align = FFMAX(w_align, 8);
  273. }
  274. *width = FFALIGN(*width, w_align);
  275. *height = FFALIGN(*height, h_align);
  276. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
  277. // some of the optimized chroma MC reads one line too much
  278. // which is also done in mpeg decoders with lowres > 0
  279. *height += 2;
  280. for (i = 0; i < 4; i++)
  281. linesize_align[i] = STRIDE_ALIGN;
  282. }
  283. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  284. {
  285. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  286. int chroma_shift = desc->log2_chroma_w;
  287. int linesize_align[AV_NUM_DATA_POINTERS];
  288. int align;
  289. avcodec_align_dimensions2(s, width, height, linesize_align);
  290. align = FFMAX(linesize_align[0], linesize_align[3]);
  291. linesize_align[1] <<= chroma_shift;
  292. linesize_align[2] <<= chroma_shift;
  293. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  294. *width = FFALIGN(*width, align);
  295. }
  296. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  297. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  298. int buf_size, int align)
  299. {
  300. int ch, planar, needed_size, ret = 0;
  301. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  302. frame->nb_samples, sample_fmt,
  303. align);
  304. if (buf_size < needed_size)
  305. return AVERROR(EINVAL);
  306. planar = av_sample_fmt_is_planar(sample_fmt);
  307. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  308. if (!(frame->extended_data = av_mallocz(nb_channels *
  309. sizeof(*frame->extended_data))))
  310. return AVERROR(ENOMEM);
  311. } else {
  312. frame->extended_data = frame->data;
  313. }
  314. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  315. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  316. sample_fmt, align)) < 0) {
  317. if (frame->extended_data != frame->data)
  318. av_freep(&frame->extended_data);
  319. return ret;
  320. }
  321. if (frame->extended_data != frame->data) {
  322. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  323. frame->data[ch] = frame->extended_data[ch];
  324. }
  325. return ret;
  326. }
  327. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  328. {
  329. FramePool *pool = avctx->internal->pool;
  330. int i, ret;
  331. switch (avctx->codec_type) {
  332. case AVMEDIA_TYPE_VIDEO: {
  333. AVPicture picture;
  334. int size[4] = { 0 };
  335. int w = frame->width;
  336. int h = frame->height;
  337. int tmpsize, unaligned;
  338. if (pool->format == frame->format &&
  339. pool->width == frame->width && pool->height == frame->height)
  340. return 0;
  341. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  342. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  343. w += EDGE_WIDTH * 2;
  344. h += EDGE_WIDTH * 2;
  345. }
  346. do {
  347. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  348. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  349. av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
  350. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  351. w += w & ~(w - 1);
  352. unaligned = 0;
  353. for (i = 0; i < 4; i++)
  354. unaligned |= picture.linesize[i] % pool->stride_align[i];
  355. } while (unaligned);
  356. tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
  357. NULL, picture.linesize);
  358. if (tmpsize < 0)
  359. return -1;
  360. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  361. size[i] = picture.data[i + 1] - picture.data[i];
  362. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  363. for (i = 0; i < 4; i++) {
  364. av_buffer_pool_uninit(&pool->pools[i]);
  365. pool->linesize[i] = picture.linesize[i];
  366. if (size[i]) {
  367. pool->pools[i] = av_buffer_pool_init(size[i] + 16,
  368. CONFIG_MEMORY_POISONING ?
  369. NULL :
  370. av_buffer_allocz);
  371. if (!pool->pools[i]) {
  372. ret = AVERROR(ENOMEM);
  373. goto fail;
  374. }
  375. }
  376. }
  377. pool->format = frame->format;
  378. pool->width = frame->width;
  379. pool->height = frame->height;
  380. break;
  381. }
  382. case AVMEDIA_TYPE_AUDIO: {
  383. int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
  384. int planar = av_sample_fmt_is_planar(frame->format);
  385. int planes = planar ? ch : 1;
  386. if (pool->format == frame->format && pool->planes == planes &&
  387. pool->channels == ch && frame->nb_samples == pool->samples)
  388. return 0;
  389. av_buffer_pool_uninit(&pool->pools[0]);
  390. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  391. frame->nb_samples, frame->format, 0);
  392. if (ret < 0)
  393. goto fail;
  394. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  395. if (!pool->pools[0]) {
  396. ret = AVERROR(ENOMEM);
  397. goto fail;
  398. }
  399. pool->format = frame->format;
  400. pool->planes = planes;
  401. pool->channels = ch;
  402. pool->samples = frame->nb_samples;
  403. break;
  404. }
  405. default: av_assert0(0);
  406. }
  407. return 0;
  408. fail:
  409. for (i = 0; i < 4; i++)
  410. av_buffer_pool_uninit(&pool->pools[i]);
  411. pool->format = -1;
  412. pool->planes = pool->channels = pool->samples = 0;
  413. pool->width = pool->height = 0;
  414. return ret;
  415. }
  416. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  417. {
  418. FramePool *pool = avctx->internal->pool;
  419. int planes = pool->planes;
  420. int i;
  421. frame->linesize[0] = pool->linesize[0];
  422. if (planes > AV_NUM_DATA_POINTERS) {
  423. frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  424. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  425. frame->extended_buf = av_mallocz(frame->nb_extended_buf *
  426. sizeof(*frame->extended_buf));
  427. if (!frame->extended_data || !frame->extended_buf) {
  428. av_freep(&frame->extended_data);
  429. av_freep(&frame->extended_buf);
  430. return AVERROR(ENOMEM);
  431. }
  432. } else {
  433. frame->extended_data = frame->data;
  434. av_assert0(frame->nb_extended_buf == 0);
  435. }
  436. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  437. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  438. if (!frame->buf[i])
  439. goto fail;
  440. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  441. }
  442. for (i = 0; i < frame->nb_extended_buf; i++) {
  443. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  444. if (!frame->extended_buf[i])
  445. goto fail;
  446. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  447. }
  448. if (avctx->debug & FF_DEBUG_BUFFERS)
  449. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  450. return 0;
  451. fail:
  452. av_frame_unref(frame);
  453. return AVERROR(ENOMEM);
  454. }
  455. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  456. {
  457. FramePool *pool = s->internal->pool;
  458. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
  459. int pixel_size = desc->comp[0].step_minus1 + 1;
  460. int h_chroma_shift, v_chroma_shift;
  461. int i;
  462. if (pic->data[0] != NULL) {
  463. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  464. return -1;
  465. }
  466. memset(pic->data, 0, sizeof(pic->data));
  467. pic->extended_data = pic->data;
  468. av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  469. for (i = 0; i < 4 && pool->pools[i]; i++) {
  470. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  471. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  472. pic->linesize[i] = pool->linesize[i];
  473. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  474. if (!pic->buf[i])
  475. goto fail;
  476. // no edge if EDGE EMU or not planar YUV
  477. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
  478. pic->data[i] = pic->buf[i]->data;
  479. else {
  480. pic->data[i] = pic->buf[i]->data +
  481. FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
  482. (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
  483. }
  484. }
  485. for (; i < AV_NUM_DATA_POINTERS; i++) {
  486. pic->data[i] = NULL;
  487. pic->linesize[i] = 0;
  488. }
  489. if (pic->data[1] && !pic->data[2])
  490. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  491. if (s->debug & FF_DEBUG_BUFFERS)
  492. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  493. return 0;
  494. fail:
  495. av_frame_unref(pic);
  496. return AVERROR(ENOMEM);
  497. }
  498. void avpriv_color_frame(AVFrame *frame, const int c[4])
  499. {
  500. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  501. int p, y, x;
  502. av_assert0(desc->flags & PIX_FMT_PLANAR);
  503. for (p = 0; p<desc->nb_components; p++) {
  504. uint8_t *dst = frame->data[p];
  505. int is_chroma = p == 1 || p == 2;
  506. int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
  507. int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
  508. for (y = 0; y < height; y++) {
  509. if (desc->comp[0].depth_minus1 >= 8) {
  510. for (x = 0; x<bytes; x++)
  511. ((uint16_t*)dst)[x] = c[p];
  512. }else
  513. memset(dst, c[p], bytes);
  514. dst += frame->linesize[p];
  515. }
  516. }
  517. }
  518. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  519. {
  520. int ret;
  521. if ((ret = update_frame_pool(avctx, frame)) < 0)
  522. return ret;
  523. #if FF_API_GET_BUFFER
  524. frame->type = FF_BUFFER_TYPE_INTERNAL;
  525. #endif
  526. switch (avctx->codec_type) {
  527. case AVMEDIA_TYPE_VIDEO:
  528. return video_get_buffer(avctx, frame);
  529. case AVMEDIA_TYPE_AUDIO:
  530. return audio_get_buffer(avctx, frame);
  531. default:
  532. return -1;
  533. }
  534. }
  535. int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
  536. {
  537. if (avctx->pkt) {
  538. frame->pkt_pts = avctx->pkt->pts;
  539. av_frame_set_pkt_pos (frame, avctx->pkt->pos);
  540. av_frame_set_pkt_duration(frame, avctx->pkt->duration);
  541. av_frame_set_pkt_size (frame, avctx->pkt->size);
  542. } else {
  543. frame->pkt_pts = AV_NOPTS_VALUE;
  544. av_frame_set_pkt_pos (frame, -1);
  545. av_frame_set_pkt_duration(frame, 0);
  546. av_frame_set_pkt_size (frame, -1);
  547. }
  548. frame->reordered_opaque = avctx->reordered_opaque;
  549. switch (avctx->codec->type) {
  550. case AVMEDIA_TYPE_VIDEO:
  551. frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
  552. frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
  553. if (frame->format < 0)
  554. frame->format = avctx->pix_fmt;
  555. if (!frame->sample_aspect_ratio.num)
  556. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  557. break;
  558. case AVMEDIA_TYPE_AUDIO:
  559. if (!frame->sample_rate)
  560. frame->sample_rate = avctx->sample_rate;
  561. if (frame->format < 0)
  562. frame->format = avctx->sample_fmt;
  563. if (!frame->channel_layout) {
  564. if (avctx->channel_layout) {
  565. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  566. avctx->channels) {
  567. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  568. "configuration.\n");
  569. return AVERROR(EINVAL);
  570. }
  571. frame->channel_layout = avctx->channel_layout;
  572. } else {
  573. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  574. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  575. avctx->channels);
  576. return AVERROR(ENOSYS);
  577. }
  578. }
  579. }
  580. av_frame_set_channels(frame, avctx->channels);
  581. break;
  582. }
  583. return 0;
  584. }
  585. #if FF_API_GET_BUFFER
  586. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  587. {
  588. return avcodec_default_get_buffer2(avctx, frame, 0);
  589. }
  590. typedef struct CompatReleaseBufPriv {
  591. AVCodecContext avctx;
  592. AVFrame frame;
  593. } CompatReleaseBufPriv;
  594. static void compat_free_buffer(void *opaque, uint8_t *data)
  595. {
  596. CompatReleaseBufPriv *priv = opaque;
  597. if (priv->avctx.release_buffer)
  598. priv->avctx.release_buffer(&priv->avctx, &priv->frame);
  599. av_freep(&priv);
  600. }
  601. static void compat_release_buffer(void *opaque, uint8_t *data)
  602. {
  603. AVBufferRef *buf = opaque;
  604. av_buffer_unref(&buf);
  605. }
  606. #endif
  607. static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
  608. {
  609. int ret;
  610. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  611. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
  612. av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  613. return AVERROR(EINVAL);
  614. }
  615. }
  616. if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
  617. return ret;
  618. #if FF_API_GET_BUFFER
  619. /*
  620. * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
  621. * We wrap each plane in its own AVBuffer. Each of those has a reference to
  622. * a dummy AVBuffer as its private data, unreffing it on free.
  623. * When all the planes are freed, the dummy buffer's free callback calls
  624. * release_buffer().
  625. */
  626. if (avctx->get_buffer) {
  627. CompatReleaseBufPriv *priv = NULL;
  628. AVBufferRef *dummy_buf = NULL;
  629. int planes, i, ret;
  630. if (flags & AV_GET_BUFFER_FLAG_REF)
  631. frame->reference = 1;
  632. ret = avctx->get_buffer(avctx, frame);
  633. if (ret < 0)
  634. return ret;
  635. /* return if the buffers are already set up
  636. * this would happen e.g. when a custom get_buffer() calls
  637. * avcodec_default_get_buffer
  638. */
  639. if (frame->buf[0])
  640. goto end;
  641. priv = av_mallocz(sizeof(*priv));
  642. if (!priv) {
  643. ret = AVERROR(ENOMEM);
  644. goto fail;
  645. }
  646. priv->avctx = *avctx;
  647. priv->frame = *frame;
  648. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
  649. if (!dummy_buf) {
  650. ret = AVERROR(ENOMEM);
  651. goto fail;
  652. }
  653. #define WRAP_PLANE(ref_out, data, data_size) \
  654. do { \
  655. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  656. if (!dummy_ref) { \
  657. ret = AVERROR(ENOMEM); \
  658. goto fail; \
  659. } \
  660. ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
  661. dummy_ref, 0); \
  662. if (!ref_out) { \
  663. av_frame_unref(frame); \
  664. ret = AVERROR(ENOMEM); \
  665. goto fail; \
  666. } \
  667. } while (0)
  668. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  669. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  670. planes = av_pix_fmt_count_planes(frame->format);
  671. /* workaround for AVHWAccel plane count of 0, buf[0] is used as
  672. check for allocated buffers: make libavcodec happy */
  673. if (desc && desc->flags & PIX_FMT_HWACCEL)
  674. planes = 1;
  675. if (!desc || planes <= 0) {
  676. ret = AVERROR(EINVAL);
  677. goto fail;
  678. }
  679. for (i = 0; i < planes; i++) {
  680. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  681. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  682. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  683. }
  684. } else {
  685. int planar = av_sample_fmt_is_planar(frame->format);
  686. planes = planar ? avctx->channels : 1;
  687. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  688. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  689. frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
  690. frame->nb_extended_buf);
  691. if (!frame->extended_buf) {
  692. ret = AVERROR(ENOMEM);
  693. goto fail;
  694. }
  695. }
  696. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  697. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  698. for (i = 0; i < frame->nb_extended_buf; i++)
  699. WRAP_PLANE(frame->extended_buf[i],
  700. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  701. frame->linesize[0]);
  702. }
  703. av_buffer_unref(&dummy_buf);
  704. end:
  705. frame->width = avctx->width;
  706. frame->height = avctx->height;
  707. return 0;
  708. fail:
  709. avctx->release_buffer(avctx, frame);
  710. av_freep(&priv);
  711. av_buffer_unref(&dummy_buf);
  712. return ret;
  713. }
  714. #endif
  715. ret = avctx->get_buffer2(avctx, frame, flags);
  716. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  717. frame->width = avctx->width;
  718. frame->height = avctx->height;
  719. }
  720. return ret;
  721. }
  722. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  723. {
  724. int ret = get_buffer_internal(avctx, frame, flags);
  725. if (ret < 0)
  726. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  727. return ret;
  728. }
  729. static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
  730. {
  731. AVFrame tmp;
  732. int ret;
  733. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  734. if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
  735. av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  736. frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  737. av_frame_unref(frame);
  738. }
  739. ff_init_buffer_info(avctx, frame);
  740. if (!frame->data[0])
  741. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  742. if (av_frame_is_writable(frame))
  743. return 0;
  744. av_frame_move_ref(&tmp, frame);
  745. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  746. if (ret < 0) {
  747. av_frame_unref(&tmp);
  748. return ret;
  749. }
  750. av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
  751. frame->format, frame->width, frame->height);
  752. av_frame_unref(&tmp);
  753. return 0;
  754. }
  755. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  756. {
  757. int ret = reget_buffer_internal(avctx, frame);
  758. if (ret < 0)
  759. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  760. return ret;
  761. }
  762. #if FF_API_GET_BUFFER
  763. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  764. {
  765. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  766. av_frame_unref(pic);
  767. }
  768. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  769. {
  770. av_assert0(0);
  771. }
  772. #endif
  773. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  774. {
  775. int i;
  776. for (i = 0; i < count; i++) {
  777. int r = func(c, (char *)arg + i * size);
  778. if (ret)
  779. ret[i] = r;
  780. }
  781. return 0;
  782. }
  783. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  784. {
  785. int i;
  786. for (i = 0; i < count; i++) {
  787. int r = func(c, arg, i, 0);
  788. if (ret)
  789. ret[i] = r;
  790. }
  791. return 0;
  792. }
  793. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  794. {
  795. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  796. return desc->flags & PIX_FMT_HWACCEL;
  797. }
  798. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  799. {
  800. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  801. ++fmt;
  802. return fmt[0];
  803. }
  804. void avcodec_get_frame_defaults(AVFrame *frame)
  805. {
  806. #if LIBAVCODEC_VERSION_MAJOR >= 55
  807. // extended_data should explicitly be freed when needed, this code is unsafe currently
  808. // also this is not compatible to the <55 ABI/API
  809. if (frame->extended_data != frame->data && 0)
  810. av_freep(&frame->extended_data);
  811. #endif
  812. memset(frame, 0, sizeof(AVFrame));
  813. frame->pts =
  814. frame->pkt_dts =
  815. frame->pkt_pts = AV_NOPTS_VALUE;
  816. av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
  817. av_frame_set_pkt_duration (frame, 0);
  818. av_frame_set_pkt_pos (frame, -1);
  819. av_frame_set_pkt_size (frame, -1);
  820. frame->key_frame = 1;
  821. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  822. frame->format = -1; /* unknown */
  823. frame->extended_data = frame->data;
  824. }
  825. AVFrame *avcodec_alloc_frame(void)
  826. {
  827. AVFrame *frame = av_malloc(sizeof(AVFrame));
  828. if (frame == NULL)
  829. return NULL;
  830. frame->extended_data = NULL;
  831. avcodec_get_frame_defaults(frame);
  832. return frame;
  833. }
  834. void avcodec_free_frame(AVFrame **frame)
  835. {
  836. AVFrame *f;
  837. if (!frame || !*frame)
  838. return;
  839. f = *frame;
  840. if (f->extended_data != f->data)
  841. av_freep(&f->extended_data);
  842. av_freep(frame);
  843. }
  844. #define MAKE_ACCESSORS(str, name, type, field) \
  845. type av_##name##_get_##field(const str *s) { return s->field; } \
  846. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  847. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  848. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  849. MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
  850. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  851. {
  852. memset(sub, 0, sizeof(*sub));
  853. sub->pts = AV_NOPTS_VALUE;
  854. }
  855. static int get_bit_rate(AVCodecContext *ctx)
  856. {
  857. int bit_rate;
  858. int bits_per_sample;
  859. switch (ctx->codec_type) {
  860. case AVMEDIA_TYPE_VIDEO:
  861. case AVMEDIA_TYPE_DATA:
  862. case AVMEDIA_TYPE_SUBTITLE:
  863. case AVMEDIA_TYPE_ATTACHMENT:
  864. bit_rate = ctx->bit_rate;
  865. break;
  866. case AVMEDIA_TYPE_AUDIO:
  867. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  868. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  869. break;
  870. default:
  871. bit_rate = 0;
  872. break;
  873. }
  874. return bit_rate;
  875. }
  876. #if FF_API_AVCODEC_OPEN
  877. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  878. {
  879. return avcodec_open2(avctx, codec, NULL);
  880. }
  881. #endif
  882. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  883. {
  884. int ret = 0;
  885. ff_unlock_avcodec();
  886. ret = avcodec_open2(avctx, codec, options);
  887. ff_lock_avcodec(avctx);
  888. return ret;
  889. }
  890. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  891. {
  892. int ret = 0;
  893. AVDictionary *tmp = NULL;
  894. if (avcodec_is_open(avctx))
  895. return 0;
  896. if ((!codec && !avctx->codec)) {
  897. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  898. return AVERROR(EINVAL);
  899. }
  900. if ((codec && avctx->codec && codec != avctx->codec)) {
  901. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  902. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  903. return AVERROR(EINVAL);
  904. }
  905. if (!codec)
  906. codec = avctx->codec;
  907. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  908. return AVERROR(EINVAL);
  909. if (options)
  910. av_dict_copy(&tmp, *options, 0);
  911. ret = ff_lock_avcodec(avctx);
  912. if (ret < 0)
  913. return ret;
  914. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  915. if (!avctx->internal) {
  916. ret = AVERROR(ENOMEM);
  917. goto end;
  918. }
  919. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  920. if (!avctx->internal->pool) {
  921. ret = AVERROR(ENOMEM);
  922. goto free_and_end;
  923. }
  924. if (codec->priv_data_size > 0) {
  925. if (!avctx->priv_data) {
  926. avctx->priv_data = av_mallocz(codec->priv_data_size);
  927. if (!avctx->priv_data) {
  928. ret = AVERROR(ENOMEM);
  929. goto end;
  930. }
  931. if (codec->priv_class) {
  932. *(const AVClass **)avctx->priv_data = codec->priv_class;
  933. av_opt_set_defaults(avctx->priv_data);
  934. }
  935. }
  936. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  937. goto free_and_end;
  938. } else {
  939. avctx->priv_data = NULL;
  940. }
  941. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  942. goto free_and_end;
  943. // only call avcodec_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
  944. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  945. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
  946. if (avctx->coded_width && avctx->coded_height)
  947. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  948. else if (avctx->width && avctx->height)
  949. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  950. }
  951. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  952. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  953. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  954. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  955. avcodec_set_dimensions(avctx, 0, 0);
  956. }
  957. /* if the decoder init function was already called previously,
  958. * free the already allocated subtitle_header before overwriting it */
  959. if (av_codec_is_decoder(codec))
  960. av_freep(&avctx->subtitle_header);
  961. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  962. ret = AVERROR(EINVAL);
  963. goto free_and_end;
  964. }
  965. avctx->codec = codec;
  966. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  967. avctx->codec_id == AV_CODEC_ID_NONE) {
  968. avctx->codec_type = codec->type;
  969. avctx->codec_id = codec->id;
  970. }
  971. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  972. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  973. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  974. ret = AVERROR(EINVAL);
  975. goto free_and_end;
  976. }
  977. avctx->frame_number = 0;
  978. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  979. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  980. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  981. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  982. AVCodec *codec2;
  983. av_log(avctx, AV_LOG_ERROR,
  984. "The %s '%s' is experimental but experimental codecs are not enabled, "
  985. "add '-strict %d' if you want to use it.\n",
  986. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  987. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  988. if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
  989. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  990. codec_string, codec2->name);
  991. ret = AVERROR_EXPERIMENTAL;
  992. goto free_and_end;
  993. }
  994. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  995. (!avctx->time_base.num || !avctx->time_base.den)) {
  996. avctx->time_base.num = 1;
  997. avctx->time_base.den = avctx->sample_rate;
  998. }
  999. if (!HAVE_THREADS)
  1000. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  1001. if (CONFIG_FRAME_THREAD_ENCODER) {
  1002. ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  1003. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  1004. ff_lock_avcodec(avctx);
  1005. if (ret < 0)
  1006. goto free_and_end;
  1007. }
  1008. if (HAVE_THREADS && !avctx->thread_opaque
  1009. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  1010. ret = ff_thread_init(avctx);
  1011. if (ret < 0) {
  1012. goto free_and_end;
  1013. }
  1014. }
  1015. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  1016. avctx->thread_count = 1;
  1017. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  1018. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  1019. avctx->codec->max_lowres);
  1020. ret = AVERROR(EINVAL);
  1021. goto free_and_end;
  1022. }
  1023. if (av_codec_is_encoder(avctx->codec)) {
  1024. int i;
  1025. if (avctx->codec->sample_fmts) {
  1026. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  1027. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  1028. break;
  1029. if (avctx->channels == 1 &&
  1030. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  1031. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  1032. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  1033. break;
  1034. }
  1035. }
  1036. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  1037. char buf[128];
  1038. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  1039. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  1040. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  1041. ret = AVERROR(EINVAL);
  1042. goto free_and_end;
  1043. }
  1044. }
  1045. if (avctx->codec->pix_fmts) {
  1046. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  1047. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  1048. break;
  1049. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  1050. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  1051. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  1052. char buf[128];
  1053. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  1054. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  1055. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  1056. ret = AVERROR(EINVAL);
  1057. goto free_and_end;
  1058. }
  1059. }
  1060. if (avctx->codec->supported_samplerates) {
  1061. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1062. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1063. break;
  1064. if (avctx->codec->supported_samplerates[i] == 0) {
  1065. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1066. avctx->sample_rate);
  1067. ret = AVERROR(EINVAL);
  1068. goto free_and_end;
  1069. }
  1070. }
  1071. if (avctx->codec->channel_layouts) {
  1072. if (!avctx->channel_layout) {
  1073. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  1074. } else {
  1075. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1076. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1077. break;
  1078. if (avctx->codec->channel_layouts[i] == 0) {
  1079. char buf[512];
  1080. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1081. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  1082. ret = AVERROR(EINVAL);
  1083. goto free_and_end;
  1084. }
  1085. }
  1086. }
  1087. if (avctx->channel_layout && avctx->channels) {
  1088. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1089. if (channels != avctx->channels) {
  1090. char buf[512];
  1091. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1092. av_log(avctx, AV_LOG_ERROR,
  1093. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  1094. buf, channels, avctx->channels);
  1095. ret = AVERROR(EINVAL);
  1096. goto free_and_end;
  1097. }
  1098. } else if (avctx->channel_layout) {
  1099. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1100. }
  1101. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
  1102. avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
  1103. ) {
  1104. if (avctx->width <= 0 || avctx->height <= 0) {
  1105. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  1106. ret = AVERROR(EINVAL);
  1107. goto free_and_end;
  1108. }
  1109. }
  1110. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  1111. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  1112. av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
  1113. }
  1114. if (!avctx->rc_initial_buffer_occupancy)
  1115. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1116. }
  1117. avctx->pts_correction_num_faulty_pts =
  1118. avctx->pts_correction_num_faulty_dts = 0;
  1119. avctx->pts_correction_last_pts =
  1120. avctx->pts_correction_last_dts = INT64_MIN;
  1121. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  1122. || avctx->internal->frame_thread_encoder)) {
  1123. ret = avctx->codec->init(avctx);
  1124. if (ret < 0) {
  1125. goto free_and_end;
  1126. }
  1127. }
  1128. ret=0;
  1129. if (av_codec_is_decoder(avctx->codec)) {
  1130. if (!avctx->bit_rate)
  1131. avctx->bit_rate = get_bit_rate(avctx);
  1132. /* validate channel layout from the decoder */
  1133. if (avctx->channel_layout) {
  1134. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1135. if (!avctx->channels)
  1136. avctx->channels = channels;
  1137. else if (channels != avctx->channels) {
  1138. char buf[512];
  1139. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1140. av_log(avctx, AV_LOG_WARNING,
  1141. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  1142. "ignoring specified channel layout\n",
  1143. buf, channels, avctx->channels);
  1144. avctx->channel_layout = 0;
  1145. }
  1146. }
  1147. if (avctx->channels && avctx->channels < 0 ||
  1148. avctx->channels > FF_SANE_NB_CHANNELS) {
  1149. ret = AVERROR(EINVAL);
  1150. goto free_and_end;
  1151. }
  1152. if (avctx->sub_charenc) {
  1153. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1154. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1155. "supported with subtitles codecs\n");
  1156. ret = AVERROR(EINVAL);
  1157. goto free_and_end;
  1158. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1159. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1160. "subtitles character encoding will be ignored\n",
  1161. avctx->codec_descriptor->name);
  1162. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1163. } else {
  1164. /* input character encoding is set for a text based subtitle
  1165. * codec at this point */
  1166. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1167. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1168. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1169. #if CONFIG_ICONV
  1170. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1171. if (cd == (iconv_t)-1) {
  1172. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1173. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1174. ret = AVERROR(errno);
  1175. goto free_and_end;
  1176. }
  1177. iconv_close(cd);
  1178. #else
  1179. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1180. "conversion needs a libavcodec built with iconv support "
  1181. "for this codec\n");
  1182. ret = AVERROR(ENOSYS);
  1183. goto free_and_end;
  1184. #endif
  1185. }
  1186. }
  1187. }
  1188. }
  1189. end:
  1190. ff_unlock_avcodec();
  1191. if (options) {
  1192. av_dict_free(options);
  1193. *options = tmp;
  1194. }
  1195. return ret;
  1196. free_and_end:
  1197. av_dict_free(&tmp);
  1198. av_freep(&avctx->priv_data);
  1199. if (avctx->internal)
  1200. av_freep(&avctx->internal->pool);
  1201. av_freep(&avctx->internal);
  1202. avctx->codec = NULL;
  1203. goto end;
  1204. }
  1205. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  1206. {
  1207. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  1208. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  1209. return AVERROR(EINVAL);
  1210. }
  1211. if (avctx) {
  1212. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1213. if (!avpkt->data || avpkt->size < size) {
  1214. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1215. avpkt->data = avctx->internal->byte_buffer;
  1216. avpkt->size = avctx->internal->byte_buffer_size;
  1217. avpkt->destruct = NULL;
  1218. }
  1219. }
  1220. if (avpkt->data) {
  1221. AVBufferRef *buf = avpkt->buf;
  1222. #if FF_API_DESTRUCT_PACKET
  1223. void *destruct = avpkt->destruct;
  1224. #endif
  1225. if (avpkt->size < size) {
  1226. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  1227. return AVERROR(EINVAL);
  1228. }
  1229. av_init_packet(avpkt);
  1230. #if FF_API_DESTRUCT_PACKET
  1231. avpkt->destruct = destruct;
  1232. #endif
  1233. avpkt->buf = buf;
  1234. avpkt->size = size;
  1235. return 0;
  1236. } else {
  1237. int ret = av_new_packet(avpkt, size);
  1238. if (ret < 0)
  1239. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  1240. return ret;
  1241. }
  1242. }
  1243. int ff_alloc_packet(AVPacket *avpkt, int size)
  1244. {
  1245. return ff_alloc_packet2(NULL, avpkt, size);
  1246. }
  1247. /**
  1248. * Pad last frame with silence.
  1249. */
  1250. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1251. {
  1252. AVFrame *frame = NULL;
  1253. uint8_t *buf = NULL;
  1254. int ret;
  1255. if (!(frame = avcodec_alloc_frame()))
  1256. return AVERROR(ENOMEM);
  1257. *frame = *src;
  1258. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  1259. s->frame_size, s->sample_fmt, 0)) < 0)
  1260. goto fail;
  1261. if (!(buf = av_malloc(ret))) {
  1262. ret = AVERROR(ENOMEM);
  1263. goto fail;
  1264. }
  1265. frame->nb_samples = s->frame_size;
  1266. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  1267. buf, ret, 0)) < 0)
  1268. goto fail;
  1269. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1270. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1271. goto fail;
  1272. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1273. frame->nb_samples - src->nb_samples,
  1274. s->channels, s->sample_fmt)) < 0)
  1275. goto fail;
  1276. *dst = frame;
  1277. return 0;
  1278. fail:
  1279. if (frame->extended_data != frame->data)
  1280. av_freep(&frame->extended_data);
  1281. av_freep(&buf);
  1282. av_freep(&frame);
  1283. return ret;
  1284. }
  1285. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1286. AVPacket *avpkt,
  1287. const AVFrame *frame,
  1288. int *got_packet_ptr)
  1289. {
  1290. AVFrame tmp;
  1291. AVFrame *padded_frame = NULL;
  1292. int ret;
  1293. AVPacket user_pkt = *avpkt;
  1294. int needs_realloc = !user_pkt.data;
  1295. *got_packet_ptr = 0;
  1296. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1297. av_free_packet(avpkt);
  1298. av_init_packet(avpkt);
  1299. return 0;
  1300. }
  1301. /* ensure that extended_data is properly set */
  1302. if (frame && !frame->extended_data) {
  1303. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1304. avctx->channels > AV_NUM_DATA_POINTERS) {
  1305. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1306. "with more than %d channels, but extended_data is not set.\n",
  1307. AV_NUM_DATA_POINTERS);
  1308. return AVERROR(EINVAL);
  1309. }
  1310. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1311. tmp = *frame;
  1312. tmp.extended_data = tmp.data;
  1313. frame = &tmp;
  1314. }
  1315. /* check for valid frame size */
  1316. if (frame) {
  1317. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1318. if (frame->nb_samples > avctx->frame_size) {
  1319. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1320. return AVERROR(EINVAL);
  1321. }
  1322. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1323. if (frame->nb_samples < avctx->frame_size &&
  1324. !avctx->internal->last_audio_frame) {
  1325. ret = pad_last_frame(avctx, &padded_frame, frame);
  1326. if (ret < 0)
  1327. return ret;
  1328. frame = padded_frame;
  1329. avctx->internal->last_audio_frame = 1;
  1330. }
  1331. if (frame->nb_samples != avctx->frame_size) {
  1332. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1333. ret = AVERROR(EINVAL);
  1334. goto end;
  1335. }
  1336. }
  1337. }
  1338. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1339. if (!ret) {
  1340. if (*got_packet_ptr) {
  1341. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1342. if (avpkt->pts == AV_NOPTS_VALUE)
  1343. avpkt->pts = frame->pts;
  1344. if (!avpkt->duration)
  1345. avpkt->duration = ff_samples_to_time_base(avctx,
  1346. frame->nb_samples);
  1347. }
  1348. avpkt->dts = avpkt->pts;
  1349. } else {
  1350. avpkt->size = 0;
  1351. }
  1352. }
  1353. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1354. needs_realloc = 0;
  1355. if (user_pkt.data) {
  1356. if (user_pkt.size >= avpkt->size) {
  1357. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1358. } else {
  1359. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1360. avpkt->size = user_pkt.size;
  1361. ret = -1;
  1362. }
  1363. avpkt->buf = user_pkt.buf;
  1364. avpkt->data = user_pkt.data;
  1365. avpkt->destruct = user_pkt.destruct;
  1366. } else {
  1367. if (av_dup_packet(avpkt) < 0) {
  1368. ret = AVERROR(ENOMEM);
  1369. }
  1370. }
  1371. }
  1372. if (!ret) {
  1373. if (needs_realloc && avpkt->data) {
  1374. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1375. if (ret >= 0)
  1376. avpkt->data = avpkt->buf->data;
  1377. }
  1378. avctx->frame_number++;
  1379. }
  1380. if (ret < 0 || !*got_packet_ptr) {
  1381. av_free_packet(avpkt);
  1382. av_init_packet(avpkt);
  1383. goto end;
  1384. }
  1385. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1386. * this needs to be moved to the encoders, but for now we can do it
  1387. * here to simplify things */
  1388. avpkt->flags |= AV_PKT_FLAG_KEY;
  1389. end:
  1390. if (padded_frame) {
  1391. av_freep(&padded_frame->data[0]);
  1392. if (padded_frame->extended_data != padded_frame->data)
  1393. av_freep(&padded_frame->extended_data);
  1394. av_freep(&padded_frame);
  1395. }
  1396. return ret;
  1397. }
  1398. #if FF_API_OLD_ENCODE_AUDIO
  1399. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1400. uint8_t *buf, int buf_size,
  1401. const short *samples)
  1402. {
  1403. AVPacket pkt;
  1404. AVFrame frame0 = { { 0 } };
  1405. AVFrame *frame;
  1406. int ret, samples_size, got_packet;
  1407. av_init_packet(&pkt);
  1408. pkt.data = buf;
  1409. pkt.size = buf_size;
  1410. if (samples) {
  1411. frame = &frame0;
  1412. avcodec_get_frame_defaults(frame);
  1413. if (avctx->frame_size) {
  1414. frame->nb_samples = avctx->frame_size;
  1415. } else {
  1416. /* if frame_size is not set, the number of samples must be
  1417. * calculated from the buffer size */
  1418. int64_t nb_samples;
  1419. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1420. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1421. "support this codec\n");
  1422. return AVERROR(EINVAL);
  1423. }
  1424. nb_samples = (int64_t)buf_size * 8 /
  1425. (av_get_bits_per_sample(avctx->codec_id) *
  1426. avctx->channels);
  1427. if (nb_samples >= INT_MAX)
  1428. return AVERROR(EINVAL);
  1429. frame->nb_samples = nb_samples;
  1430. }
  1431. /* it is assumed that the samples buffer is large enough based on the
  1432. * relevant parameters */
  1433. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1434. frame->nb_samples,
  1435. avctx->sample_fmt, 1);
  1436. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1437. avctx->sample_fmt,
  1438. (const uint8_t *)samples,
  1439. samples_size, 1)) < 0)
  1440. return ret;
  1441. /* fabricate frame pts from sample count.
  1442. * this is needed because the avcodec_encode_audio() API does not have
  1443. * a way for the user to provide pts */
  1444. if (avctx->sample_rate && avctx->time_base.num)
  1445. frame->pts = ff_samples_to_time_base(avctx,
  1446. avctx->internal->sample_count);
  1447. else
  1448. frame->pts = AV_NOPTS_VALUE;
  1449. avctx->internal->sample_count += frame->nb_samples;
  1450. } else {
  1451. frame = NULL;
  1452. }
  1453. got_packet = 0;
  1454. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1455. if (!ret && got_packet && avctx->coded_frame) {
  1456. avctx->coded_frame->pts = pkt.pts;
  1457. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1458. }
  1459. /* free any side data since we cannot return it */
  1460. ff_packet_free_side_data(&pkt);
  1461. if (frame && frame->extended_data != frame->data)
  1462. av_freep(&frame->extended_data);
  1463. return ret ? ret : pkt.size;
  1464. }
  1465. #endif
  1466. #if FF_API_OLD_ENCODE_VIDEO
  1467. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1468. const AVFrame *pict)
  1469. {
  1470. AVPacket pkt;
  1471. int ret, got_packet = 0;
  1472. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1473. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1474. return -1;
  1475. }
  1476. av_init_packet(&pkt);
  1477. pkt.data = buf;
  1478. pkt.size = buf_size;
  1479. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1480. if (!ret && got_packet && avctx->coded_frame) {
  1481. avctx->coded_frame->pts = pkt.pts;
  1482. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1483. }
  1484. /* free any side data since we cannot return it */
  1485. if (pkt.side_data_elems > 0) {
  1486. int i;
  1487. for (i = 0; i < pkt.side_data_elems; i++)
  1488. av_free(pkt.side_data[i].data);
  1489. av_freep(&pkt.side_data);
  1490. pkt.side_data_elems = 0;
  1491. }
  1492. return ret ? ret : pkt.size;
  1493. }
  1494. #endif
  1495. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1496. AVPacket *avpkt,
  1497. const AVFrame *frame,
  1498. int *got_packet_ptr)
  1499. {
  1500. int ret;
  1501. AVPacket user_pkt = *avpkt;
  1502. int needs_realloc = !user_pkt.data;
  1503. *got_packet_ptr = 0;
  1504. if(CONFIG_FRAME_THREAD_ENCODER &&
  1505. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1506. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1507. if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
  1508. avctx->stats_out[0] = '\0';
  1509. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1510. av_free_packet(avpkt);
  1511. av_init_packet(avpkt);
  1512. avpkt->size = 0;
  1513. return 0;
  1514. }
  1515. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1516. return AVERROR(EINVAL);
  1517. av_assert0(avctx->codec->encode2);
  1518. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1519. av_assert0(ret <= 0);
  1520. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1521. needs_realloc = 0;
  1522. if (user_pkt.data) {
  1523. if (user_pkt.size >= avpkt->size) {
  1524. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1525. } else {
  1526. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1527. avpkt->size = user_pkt.size;
  1528. ret = -1;
  1529. }
  1530. avpkt->buf = user_pkt.buf;
  1531. avpkt->data = user_pkt.data;
  1532. avpkt->destruct = user_pkt.destruct;
  1533. } else {
  1534. if (av_dup_packet(avpkt) < 0) {
  1535. ret = AVERROR(ENOMEM);
  1536. }
  1537. }
  1538. }
  1539. if (!ret) {
  1540. if (!*got_packet_ptr)
  1541. avpkt->size = 0;
  1542. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1543. avpkt->pts = avpkt->dts = frame->pts;
  1544. if (needs_realloc && avpkt->data) {
  1545. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1546. if (ret >= 0)
  1547. avpkt->data = avpkt->buf->data;
  1548. }
  1549. avctx->frame_number++;
  1550. }
  1551. if (ret < 0 || !*got_packet_ptr)
  1552. av_free_packet(avpkt);
  1553. else
  1554. av_packet_merge_side_data(avpkt);
  1555. emms_c();
  1556. return ret;
  1557. }
  1558. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1559. const AVSubtitle *sub)
  1560. {
  1561. int ret;
  1562. if (sub->start_display_time) {
  1563. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1564. return -1;
  1565. }
  1566. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1567. avctx->frame_number++;
  1568. return ret;
  1569. }
  1570. /**
  1571. * Attempt to guess proper monotonic timestamps for decoded video frames
  1572. * which might have incorrect times. Input timestamps may wrap around, in
  1573. * which case the output will as well.
  1574. *
  1575. * @param pts the pts field of the decoded AVPacket, as passed through
  1576. * AVFrame.pkt_pts
  1577. * @param dts the dts field of the decoded AVPacket
  1578. * @return one of the input values, may be AV_NOPTS_VALUE
  1579. */
  1580. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1581. int64_t reordered_pts, int64_t dts)
  1582. {
  1583. int64_t pts = AV_NOPTS_VALUE;
  1584. if (dts != AV_NOPTS_VALUE) {
  1585. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1586. ctx->pts_correction_last_dts = dts;
  1587. }
  1588. if (reordered_pts != AV_NOPTS_VALUE) {
  1589. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1590. ctx->pts_correction_last_pts = reordered_pts;
  1591. }
  1592. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1593. && reordered_pts != AV_NOPTS_VALUE)
  1594. pts = reordered_pts;
  1595. else
  1596. pts = dts;
  1597. return pts;
  1598. }
  1599. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1600. {
  1601. int size = 0;
  1602. const uint8_t *data;
  1603. uint32_t flags;
  1604. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1605. return;
  1606. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1607. if (!data || size < 4)
  1608. return;
  1609. flags = bytestream_get_le32(&data);
  1610. size -= 4;
  1611. if (size < 4) /* Required for any of the changes */
  1612. return;
  1613. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1614. avctx->channels = bytestream_get_le32(&data);
  1615. size -= 4;
  1616. }
  1617. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1618. if (size < 8)
  1619. return;
  1620. avctx->channel_layout = bytestream_get_le64(&data);
  1621. size -= 8;
  1622. }
  1623. if (size < 4)
  1624. return;
  1625. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1626. avctx->sample_rate = bytestream_get_le32(&data);
  1627. size -= 4;
  1628. }
  1629. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1630. if (size < 8)
  1631. return;
  1632. avctx->width = bytestream_get_le32(&data);
  1633. avctx->height = bytestream_get_le32(&data);
  1634. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1635. size -= 8;
  1636. }
  1637. }
  1638. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  1639. {
  1640. int size, ret = 0;
  1641. const uint8_t *side_metadata;
  1642. const uint8_t *end;
  1643. side_metadata = av_packet_get_side_data(avctx->pkt,
  1644. AV_PKT_DATA_STRINGS_METADATA, &size);
  1645. if (!side_metadata)
  1646. goto end;
  1647. end = side_metadata + size;
  1648. while (side_metadata < end) {
  1649. const uint8_t *key = side_metadata;
  1650. const uint8_t *val = side_metadata + strlen(key) + 1;
  1651. int ret = av_dict_set(avpriv_frame_get_metadatap(frame), key, val, 0);
  1652. if (ret < 0)
  1653. break;
  1654. side_metadata = val + strlen(val) + 1;
  1655. }
  1656. end:
  1657. return ret;
  1658. }
  1659. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1660. int *got_picture_ptr,
  1661. const AVPacket *avpkt)
  1662. {
  1663. AVCodecInternal *avci = avctx->internal;
  1664. int ret;
  1665. // copy to ensure we do not change avpkt
  1666. AVPacket tmp = *avpkt;
  1667. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1668. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1669. return AVERROR(EINVAL);
  1670. }
  1671. *got_picture_ptr = 0;
  1672. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1673. return AVERROR(EINVAL);
  1674. avcodec_get_frame_defaults(picture);
  1675. if (!avctx->refcounted_frames)
  1676. av_frame_unref(&avci->to_free);
  1677. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1678. int did_split = av_packet_split_side_data(&tmp);
  1679. apply_param_change(avctx, &tmp);
  1680. avctx->pkt = &tmp;
  1681. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1682. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1683. &tmp);
  1684. else {
  1685. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1686. &tmp);
  1687. picture->pkt_dts = avpkt->dts;
  1688. if(!avctx->has_b_frames){
  1689. av_frame_set_pkt_pos(picture, avpkt->pos);
  1690. }
  1691. //FIXME these should be under if(!avctx->has_b_frames)
  1692. /* get_buffer is supposed to set frame parameters */
  1693. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1694. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1695. if (!picture->width) picture->width = avctx->width;
  1696. if (!picture->height) picture->height = avctx->height;
  1697. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1698. }
  1699. }
  1700. add_metadata_from_side_data(avctx, picture);
  1701. emms_c(); //needed to avoid an emms_c() call before every return;
  1702. avctx->pkt = NULL;
  1703. if (did_split) {
  1704. ff_packet_free_side_data(&tmp);
  1705. if(ret == tmp.size)
  1706. ret = avpkt->size;
  1707. }
  1708. if (ret < 0 && picture->data[0])
  1709. av_frame_unref(picture);
  1710. if (*got_picture_ptr) {
  1711. if (!avctx->refcounted_frames) {
  1712. avci->to_free = *picture;
  1713. avci->to_free.extended_data = avci->to_free.data;
  1714. memset(picture->buf, 0, sizeof(picture->buf));
  1715. }
  1716. avctx->frame_number++;
  1717. av_frame_set_best_effort_timestamp(picture,
  1718. guess_correct_pts(avctx,
  1719. picture->pkt_pts,
  1720. picture->pkt_dts));
  1721. }
  1722. } else
  1723. ret = 0;
  1724. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1725. * make sure it's set correctly */
  1726. picture->extended_data = picture->data;
  1727. return ret;
  1728. }
  1729. #if FF_API_OLD_DECODE_AUDIO
  1730. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1731. int *frame_size_ptr,
  1732. AVPacket *avpkt)
  1733. {
  1734. AVFrame frame = { { 0 } };
  1735. int ret, got_frame = 0;
  1736. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1737. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1738. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1739. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1740. "avcodec_decode_audio4()\n");
  1741. avctx->get_buffer = avcodec_default_get_buffer;
  1742. avctx->release_buffer = avcodec_default_release_buffer;
  1743. }
  1744. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1745. if (ret >= 0 && got_frame) {
  1746. int ch, plane_size;
  1747. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1748. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1749. frame.nb_samples,
  1750. avctx->sample_fmt, 1);
  1751. if (*frame_size_ptr < data_size) {
  1752. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1753. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1754. return AVERROR(EINVAL);
  1755. }
  1756. memcpy(samples, frame.extended_data[0], plane_size);
  1757. if (planar && avctx->channels > 1) {
  1758. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1759. for (ch = 1; ch < avctx->channels; ch++) {
  1760. memcpy(out, frame.extended_data[ch], plane_size);
  1761. out += plane_size;
  1762. }
  1763. }
  1764. *frame_size_ptr = data_size;
  1765. } else {
  1766. *frame_size_ptr = 0;
  1767. }
  1768. return ret;
  1769. }
  1770. #endif
  1771. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1772. AVFrame *frame,
  1773. int *got_frame_ptr,
  1774. const AVPacket *avpkt)
  1775. {
  1776. AVCodecInternal *avci = avctx->internal;
  1777. int planar, channels;
  1778. int ret = 0;
  1779. *got_frame_ptr = 0;
  1780. if (!avpkt->data && avpkt->size) {
  1781. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1782. return AVERROR(EINVAL);
  1783. }
  1784. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1785. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1786. return AVERROR(EINVAL);
  1787. }
  1788. avcodec_get_frame_defaults(frame);
  1789. if (!avctx->refcounted_frames)
  1790. av_frame_unref(&avci->to_free);
  1791. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1792. uint8_t *side;
  1793. int side_size;
  1794. // copy to ensure we do not change avpkt
  1795. AVPacket tmp = *avpkt;
  1796. int did_split = av_packet_split_side_data(&tmp);
  1797. apply_param_change(avctx, &tmp);
  1798. avctx->pkt = &tmp;
  1799. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1800. if (ret >= 0 && *got_frame_ptr) {
  1801. add_metadata_from_side_data(avctx, frame);
  1802. avctx->frame_number++;
  1803. frame->pkt_dts = avpkt->dts;
  1804. av_frame_set_best_effort_timestamp(frame,
  1805. guess_correct_pts(avctx,
  1806. frame->pkt_pts,
  1807. frame->pkt_dts));
  1808. if (frame->format == AV_SAMPLE_FMT_NONE)
  1809. frame->format = avctx->sample_fmt;
  1810. if (!frame->channel_layout)
  1811. frame->channel_layout = avctx->channel_layout;
  1812. if (!av_frame_get_channels(frame))
  1813. av_frame_set_channels(frame, avctx->channels);
  1814. if (!frame->sample_rate)
  1815. frame->sample_rate = avctx->sample_rate;
  1816. }
  1817. side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1818. if(side && side_size>=10) {
  1819. avctx->internal->skip_samples = AV_RL32(side);
  1820. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1821. avctx->internal->skip_samples);
  1822. }
  1823. if (avctx->internal->skip_samples && *got_frame_ptr) {
  1824. if(frame->nb_samples <= avctx->internal->skip_samples){
  1825. *got_frame_ptr = 0;
  1826. avctx->internal->skip_samples -= frame->nb_samples;
  1827. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1828. avctx->internal->skip_samples);
  1829. } else {
  1830. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1831. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1832. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1833. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1834. (AVRational){1, avctx->sample_rate},
  1835. avctx->pkt_timebase);
  1836. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1837. frame->pkt_pts += diff_ts;
  1838. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1839. frame->pkt_dts += diff_ts;
  1840. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  1841. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  1842. } else {
  1843. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1844. }
  1845. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1846. avctx->internal->skip_samples, frame->nb_samples);
  1847. frame->nb_samples -= avctx->internal->skip_samples;
  1848. avctx->internal->skip_samples = 0;
  1849. }
  1850. }
  1851. avctx->pkt = NULL;
  1852. if (did_split) {
  1853. ff_packet_free_side_data(&tmp);
  1854. if(ret == tmp.size)
  1855. ret = avpkt->size;
  1856. }
  1857. if (ret >= 0 && *got_frame_ptr) {
  1858. if (!avctx->refcounted_frames) {
  1859. avci->to_free = *frame;
  1860. avci->to_free.extended_data = avci->to_free.data;
  1861. memset(frame->buf, 0, sizeof(frame->buf));
  1862. frame->extended_buf = NULL;
  1863. frame->nb_extended_buf = 0;
  1864. }
  1865. } else if (frame->data[0])
  1866. av_frame_unref(frame);
  1867. }
  1868. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1869. * make sure it's set correctly; assume decoders that actually use
  1870. * extended_data are doing it correctly */
  1871. if (*got_frame_ptr) {
  1872. planar = av_sample_fmt_is_planar(frame->format);
  1873. channels = av_frame_get_channels(frame);
  1874. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1875. frame->extended_data = frame->data;
  1876. } else {
  1877. frame->extended_data = NULL;
  1878. }
  1879. return ret;
  1880. }
  1881. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  1882. static int recode_subtitle(AVCodecContext *avctx,
  1883. AVPacket *outpkt, const AVPacket *inpkt)
  1884. {
  1885. #if CONFIG_ICONV
  1886. iconv_t cd = (iconv_t)-1;
  1887. int ret = 0;
  1888. char *inb, *outb;
  1889. size_t inl, outl;
  1890. AVPacket tmp;
  1891. #endif
  1892. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER)
  1893. return 0;
  1894. #if CONFIG_ICONV
  1895. cd = iconv_open("UTF-8", avctx->sub_charenc);
  1896. av_assert0(cd != (iconv_t)-1);
  1897. inb = inpkt->data;
  1898. inl = inpkt->size;
  1899. if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
  1900. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  1901. ret = AVERROR(ENOMEM);
  1902. goto end;
  1903. }
  1904. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  1905. if (ret < 0)
  1906. goto end;
  1907. outpkt->buf = tmp.buf;
  1908. outpkt->data = tmp.data;
  1909. outpkt->size = tmp.size;
  1910. outb = outpkt->data;
  1911. outl = outpkt->size;
  1912. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  1913. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  1914. outl >= outpkt->size || inl != 0) {
  1915. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  1916. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  1917. av_free_packet(&tmp);
  1918. ret = AVERROR(errno);
  1919. goto end;
  1920. }
  1921. outpkt->size -= outl;
  1922. memset(outpkt->data + outpkt->size, 0, outl);
  1923. end:
  1924. if (cd != (iconv_t)-1)
  1925. iconv_close(cd);
  1926. return ret;
  1927. #else
  1928. av_assert0(!"requesting subtitles recoding without iconv");
  1929. #endif
  1930. }
  1931. static int utf8_check(const uint8_t *str)
  1932. {
  1933. const uint8_t *byte;
  1934. uint32_t codepoint, min;
  1935. while (*str) {
  1936. byte = str;
  1937. GET_UTF8(codepoint, *(byte++), return 0;);
  1938. min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  1939. 1 << (5 * (byte - str) - 4);
  1940. if (codepoint < min || codepoint >= 0x110000 ||
  1941. codepoint == 0xFFFE /* BOM */ ||
  1942. codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  1943. return 0;
  1944. str = byte;
  1945. }
  1946. return 1;
  1947. }
  1948. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1949. int *got_sub_ptr,
  1950. AVPacket *avpkt)
  1951. {
  1952. int i, ret = 0;
  1953. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1954. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1955. return AVERROR(EINVAL);
  1956. }
  1957. *got_sub_ptr = 0;
  1958. avcodec_get_subtitle_defaults(sub);
  1959. if (avpkt->size) {
  1960. AVPacket pkt_recoded;
  1961. AVPacket tmp = *avpkt;
  1962. int did_split = av_packet_split_side_data(&tmp);
  1963. //apply_param_change(avctx, &tmp);
  1964. pkt_recoded = tmp;
  1965. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  1966. if (ret < 0) {
  1967. *got_sub_ptr = 0;
  1968. } else {
  1969. avctx->pkt = &pkt_recoded;
  1970. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  1971. sub->pts = av_rescale_q(avpkt->pts,
  1972. avctx->pkt_timebase, AV_TIME_BASE_Q);
  1973. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  1974. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  1975. !!*got_sub_ptr >= !!sub->num_rects);
  1976. if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  1977. avctx->pkt_timebase.num) {
  1978. AVRational ms = { 1, 1000 };
  1979. sub->end_display_time = av_rescale_q(avpkt->duration,
  1980. avctx->pkt_timebase, ms);
  1981. }
  1982. for (i = 0; i < sub->num_rects; i++) {
  1983. if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  1984. av_log(avctx, AV_LOG_ERROR,
  1985. "Invalid UTF-8 in decoded subtitles text; "
  1986. "maybe missing -sub_charenc option\n");
  1987. avsubtitle_free(sub);
  1988. return AVERROR_INVALIDDATA;
  1989. }
  1990. }
  1991. if (tmp.data != pkt_recoded.data) { // did we recode?
  1992. /* prevent from destroying side data from original packet */
  1993. pkt_recoded.side_data = NULL;
  1994. pkt_recoded.side_data_elems = 0;
  1995. av_free_packet(&pkt_recoded);
  1996. }
  1997. sub->format = !(avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB);
  1998. avctx->pkt = NULL;
  1999. }
  2000. if (did_split) {
  2001. ff_packet_free_side_data(&tmp);
  2002. if(ret == tmp.size)
  2003. ret = avpkt->size;
  2004. }
  2005. if (*got_sub_ptr)
  2006. avctx->frame_number++;
  2007. }
  2008. return ret;
  2009. }
  2010. void avsubtitle_free(AVSubtitle *sub)
  2011. {
  2012. int i;
  2013. for (i = 0; i < sub->num_rects; i++) {
  2014. av_freep(&sub->rects[i]->pict.data[0]);
  2015. av_freep(&sub->rects[i]->pict.data[1]);
  2016. av_freep(&sub->rects[i]->pict.data[2]);
  2017. av_freep(&sub->rects[i]->pict.data[3]);
  2018. av_freep(&sub->rects[i]->text);
  2019. av_freep(&sub->rects[i]->ass);
  2020. av_freep(&sub->rects[i]);
  2021. }
  2022. av_freep(&sub->rects);
  2023. memset(sub, 0, sizeof(AVSubtitle));
  2024. }
  2025. av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
  2026. {
  2027. int ret = 0;
  2028. ff_unlock_avcodec();
  2029. ret = avcodec_close(avctx);
  2030. ff_lock_avcodec(NULL);
  2031. return ret;
  2032. }
  2033. av_cold int avcodec_close(AVCodecContext *avctx)
  2034. {
  2035. int ret = ff_lock_avcodec(avctx);
  2036. if (ret < 0)
  2037. return ret;
  2038. if (avcodec_is_open(avctx)) {
  2039. FramePool *pool = avctx->internal->pool;
  2040. int i;
  2041. if (CONFIG_FRAME_THREAD_ENCODER &&
  2042. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  2043. ff_unlock_avcodec();
  2044. ff_frame_thread_encoder_free(avctx);
  2045. ff_lock_avcodec(avctx);
  2046. }
  2047. if (HAVE_THREADS && avctx->thread_opaque)
  2048. ff_thread_free(avctx);
  2049. if (avctx->codec && avctx->codec->close)
  2050. avctx->codec->close(avctx);
  2051. avctx->coded_frame = NULL;
  2052. avctx->internal->byte_buffer_size = 0;
  2053. av_freep(&avctx->internal->byte_buffer);
  2054. if (!avctx->refcounted_frames)
  2055. av_frame_unref(&avctx->internal->to_free);
  2056. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  2057. av_buffer_pool_uninit(&pool->pools[i]);
  2058. av_freep(&avctx->internal->pool);
  2059. av_freep(&avctx->internal);
  2060. }
  2061. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  2062. av_opt_free(avctx->priv_data);
  2063. av_opt_free(avctx);
  2064. av_freep(&avctx->priv_data);
  2065. if (av_codec_is_encoder(avctx->codec))
  2066. av_freep(&avctx->extradata);
  2067. avctx->codec = NULL;
  2068. avctx->active_thread_type = 0;
  2069. ff_unlock_avcodec();
  2070. return 0;
  2071. }
  2072. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  2073. {
  2074. switch(id){
  2075. //This is for future deprecatec codec ids, its empty since
  2076. //last major bump but will fill up again over time, please don't remove it
  2077. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  2078. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  2079. case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
  2080. default : return id;
  2081. }
  2082. }
  2083. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  2084. {
  2085. AVCodec *p, *experimental = NULL;
  2086. p = first_avcodec;
  2087. id= remap_deprecated_codec_id(id);
  2088. while (p) {
  2089. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  2090. p->id == id) {
  2091. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  2092. experimental = p;
  2093. } else
  2094. return p;
  2095. }
  2096. p = p->next;
  2097. }
  2098. return experimental;
  2099. }
  2100. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  2101. {
  2102. return find_encdec(id, 1);
  2103. }
  2104. AVCodec *avcodec_find_encoder_by_name(const char *name)
  2105. {
  2106. AVCodec *p;
  2107. if (!name)
  2108. return NULL;
  2109. p = first_avcodec;
  2110. while (p) {
  2111. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  2112. return p;
  2113. p = p->next;
  2114. }
  2115. return NULL;
  2116. }
  2117. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  2118. {
  2119. return find_encdec(id, 0);
  2120. }
  2121. AVCodec *avcodec_find_decoder_by_name(const char *name)
  2122. {
  2123. AVCodec *p;
  2124. if (!name)
  2125. return NULL;
  2126. p = first_avcodec;
  2127. while (p) {
  2128. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  2129. return p;
  2130. p = p->next;
  2131. }
  2132. return NULL;
  2133. }
  2134. const char *avcodec_get_name(enum AVCodecID id)
  2135. {
  2136. const AVCodecDescriptor *cd;
  2137. AVCodec *codec;
  2138. if (id == AV_CODEC_ID_NONE)
  2139. return "none";
  2140. cd = avcodec_descriptor_get(id);
  2141. if (cd)
  2142. return cd->name;
  2143. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  2144. codec = avcodec_find_decoder(id);
  2145. if (codec)
  2146. return codec->name;
  2147. codec = avcodec_find_encoder(id);
  2148. if (codec)
  2149. return codec->name;
  2150. return "unknown_codec";
  2151. }
  2152. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  2153. {
  2154. int i, len, ret = 0;
  2155. #define TAG_PRINT(x) \
  2156. (((x) >= '0' && (x) <= '9') || \
  2157. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  2158. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  2159. for (i = 0; i < 4; i++) {
  2160. len = snprintf(buf, buf_size,
  2161. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  2162. buf += len;
  2163. buf_size = buf_size > len ? buf_size - len : 0;
  2164. ret += len;
  2165. codec_tag >>= 8;
  2166. }
  2167. return ret;
  2168. }
  2169. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  2170. {
  2171. const char *codec_type;
  2172. const char *codec_name;
  2173. const char *profile = NULL;
  2174. const AVCodec *p;
  2175. int bitrate;
  2176. AVRational display_aspect_ratio;
  2177. if (!buf || buf_size <= 0)
  2178. return;
  2179. codec_type = av_get_media_type_string(enc->codec_type);
  2180. codec_name = avcodec_get_name(enc->codec_id);
  2181. if (enc->profile != FF_PROFILE_UNKNOWN) {
  2182. if (enc->codec)
  2183. p = enc->codec;
  2184. else
  2185. p = encode ? avcodec_find_encoder(enc->codec_id) :
  2186. avcodec_find_decoder(enc->codec_id);
  2187. if (p)
  2188. profile = av_get_profile_name(p, enc->profile);
  2189. }
  2190. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  2191. codec_name);
  2192. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  2193. if (enc->codec && strcmp(enc->codec->name, codec_name))
  2194. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  2195. if (profile)
  2196. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  2197. if (enc->codec_tag) {
  2198. char tag_buf[32];
  2199. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  2200. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2201. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  2202. }
  2203. switch (enc->codec_type) {
  2204. case AVMEDIA_TYPE_VIDEO:
  2205. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  2206. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2207. ", %s",
  2208. av_get_pix_fmt_name(enc->pix_fmt));
  2209. if (enc->bits_per_raw_sample &&
  2210. enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
  2211. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2212. " (%d bpc)", enc->bits_per_raw_sample);
  2213. }
  2214. if (enc->width) {
  2215. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2216. ", %dx%d",
  2217. enc->width, enc->height);
  2218. if (enc->sample_aspect_ratio.num) {
  2219. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  2220. enc->width * enc->sample_aspect_ratio.num,
  2221. enc->height * enc->sample_aspect_ratio.den,
  2222. 1024 * 1024);
  2223. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2224. " [SAR %d:%d DAR %d:%d]",
  2225. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  2226. display_aspect_ratio.num, display_aspect_ratio.den);
  2227. }
  2228. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2229. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2230. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2231. ", %d/%d",
  2232. enc->time_base.num / g, enc->time_base.den / g);
  2233. }
  2234. }
  2235. if (encode) {
  2236. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2237. ", q=%d-%d", enc->qmin, enc->qmax);
  2238. }
  2239. break;
  2240. case AVMEDIA_TYPE_AUDIO:
  2241. if (enc->sample_rate) {
  2242. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2243. ", %d Hz", enc->sample_rate);
  2244. }
  2245. av_strlcat(buf, ", ", buf_size);
  2246. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  2247. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2248. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2249. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  2250. }
  2251. break;
  2252. case AVMEDIA_TYPE_DATA:
  2253. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2254. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2255. if (g)
  2256. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2257. ", %d/%d",
  2258. enc->time_base.num / g, enc->time_base.den / g);
  2259. }
  2260. break;
  2261. default:
  2262. return;
  2263. }
  2264. if (encode) {
  2265. if (enc->flags & CODEC_FLAG_PASS1)
  2266. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2267. ", pass 1");
  2268. if (enc->flags & CODEC_FLAG_PASS2)
  2269. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2270. ", pass 2");
  2271. }
  2272. bitrate = get_bit_rate(enc);
  2273. if (bitrate != 0) {
  2274. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2275. ", %d kb/s", bitrate / 1000);
  2276. }
  2277. }
  2278. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2279. {
  2280. const AVProfile *p;
  2281. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2282. return NULL;
  2283. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2284. if (p->profile == profile)
  2285. return p->name;
  2286. return NULL;
  2287. }
  2288. unsigned avcodec_version(void)
  2289. {
  2290. // av_assert0(AV_CODEC_ID_V410==164);
  2291. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2292. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2293. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2294. av_assert0(AV_CODEC_ID_SRT==94216);
  2295. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2296. av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
  2297. av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
  2298. av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
  2299. av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
  2300. av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
  2301. return LIBAVCODEC_VERSION_INT;
  2302. }
  2303. const char *avcodec_configuration(void)
  2304. {
  2305. return FFMPEG_CONFIGURATION;
  2306. }
  2307. const char *avcodec_license(void)
  2308. {
  2309. #define LICENSE_PREFIX "libavcodec license: "
  2310. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2311. }
  2312. void avcodec_flush_buffers(AVCodecContext *avctx)
  2313. {
  2314. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2315. ff_thread_flush(avctx);
  2316. else if (avctx->codec->flush)
  2317. avctx->codec->flush(avctx);
  2318. avctx->pts_correction_last_pts =
  2319. avctx->pts_correction_last_dts = INT64_MIN;
  2320. }
  2321. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2322. {
  2323. switch (codec_id) {
  2324. case AV_CODEC_ID_8SVX_EXP:
  2325. case AV_CODEC_ID_8SVX_FIB:
  2326. case AV_CODEC_ID_ADPCM_CT:
  2327. case AV_CODEC_ID_ADPCM_IMA_APC:
  2328. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2329. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2330. case AV_CODEC_ID_ADPCM_IMA_WS:
  2331. case AV_CODEC_ID_ADPCM_G722:
  2332. case AV_CODEC_ID_ADPCM_YAMAHA:
  2333. return 4;
  2334. case AV_CODEC_ID_PCM_ALAW:
  2335. case AV_CODEC_ID_PCM_MULAW:
  2336. case AV_CODEC_ID_PCM_S8:
  2337. case AV_CODEC_ID_PCM_S8_PLANAR:
  2338. case AV_CODEC_ID_PCM_U8:
  2339. case AV_CODEC_ID_PCM_ZORK:
  2340. return 8;
  2341. case AV_CODEC_ID_PCM_S16BE:
  2342. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2343. case AV_CODEC_ID_PCM_S16LE:
  2344. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2345. case AV_CODEC_ID_PCM_U16BE:
  2346. case AV_CODEC_ID_PCM_U16LE:
  2347. return 16;
  2348. case AV_CODEC_ID_PCM_S24DAUD:
  2349. case AV_CODEC_ID_PCM_S24BE:
  2350. case AV_CODEC_ID_PCM_S24LE:
  2351. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2352. case AV_CODEC_ID_PCM_U24BE:
  2353. case AV_CODEC_ID_PCM_U24LE:
  2354. return 24;
  2355. case AV_CODEC_ID_PCM_S32BE:
  2356. case AV_CODEC_ID_PCM_S32LE:
  2357. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2358. case AV_CODEC_ID_PCM_U32BE:
  2359. case AV_CODEC_ID_PCM_U32LE:
  2360. case AV_CODEC_ID_PCM_F32BE:
  2361. case AV_CODEC_ID_PCM_F32LE:
  2362. return 32;
  2363. case AV_CODEC_ID_PCM_F64BE:
  2364. case AV_CODEC_ID_PCM_F64LE:
  2365. return 64;
  2366. default:
  2367. return 0;
  2368. }
  2369. }
  2370. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2371. {
  2372. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2373. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2374. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2375. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2376. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2377. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2378. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2379. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2380. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2381. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2382. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2383. };
  2384. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2385. return AV_CODEC_ID_NONE;
  2386. if (be < 0 || be > 1)
  2387. be = AV_NE(1, 0);
  2388. return map[fmt][be];
  2389. }
  2390. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2391. {
  2392. switch (codec_id) {
  2393. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2394. return 2;
  2395. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2396. return 3;
  2397. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2398. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2399. case AV_CODEC_ID_ADPCM_IMA_QT:
  2400. case AV_CODEC_ID_ADPCM_SWF:
  2401. case AV_CODEC_ID_ADPCM_MS:
  2402. return 4;
  2403. default:
  2404. return av_get_exact_bits_per_sample(codec_id);
  2405. }
  2406. }
  2407. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2408. {
  2409. int id, sr, ch, ba, tag, bps;
  2410. id = avctx->codec_id;
  2411. sr = avctx->sample_rate;
  2412. ch = avctx->channels;
  2413. ba = avctx->block_align;
  2414. tag = avctx->codec_tag;
  2415. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2416. /* codecs with an exact constant bits per sample */
  2417. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2418. return (frame_bytes * 8LL) / (bps * ch);
  2419. bps = avctx->bits_per_coded_sample;
  2420. /* codecs with a fixed packet duration */
  2421. switch (id) {
  2422. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2423. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2424. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2425. case AV_CODEC_ID_AMR_NB:
  2426. case AV_CODEC_ID_EVRC:
  2427. case AV_CODEC_ID_GSM:
  2428. case AV_CODEC_ID_QCELP:
  2429. case AV_CODEC_ID_RA_288: return 160;
  2430. case AV_CODEC_ID_AMR_WB:
  2431. case AV_CODEC_ID_GSM_MS: return 320;
  2432. case AV_CODEC_ID_MP1: return 384;
  2433. case AV_CODEC_ID_ATRAC1: return 512;
  2434. case AV_CODEC_ID_ATRAC3: return 1024;
  2435. case AV_CODEC_ID_MP2:
  2436. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2437. case AV_CODEC_ID_AC3: return 1536;
  2438. }
  2439. if (sr > 0) {
  2440. /* calc from sample rate */
  2441. if (id == AV_CODEC_ID_TTA)
  2442. return 256 * sr / 245;
  2443. if (ch > 0) {
  2444. /* calc from sample rate and channels */
  2445. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2446. return (480 << (sr / 22050)) / ch;
  2447. }
  2448. }
  2449. if (ba > 0) {
  2450. /* calc from block_align */
  2451. if (id == AV_CODEC_ID_SIPR) {
  2452. switch (ba) {
  2453. case 20: return 160;
  2454. case 19: return 144;
  2455. case 29: return 288;
  2456. case 37: return 480;
  2457. }
  2458. } else if (id == AV_CODEC_ID_ILBC) {
  2459. switch (ba) {
  2460. case 38: return 160;
  2461. case 50: return 240;
  2462. }
  2463. }
  2464. }
  2465. if (frame_bytes > 0) {
  2466. /* calc from frame_bytes only */
  2467. if (id == AV_CODEC_ID_TRUESPEECH)
  2468. return 240 * (frame_bytes / 32);
  2469. if (id == AV_CODEC_ID_NELLYMOSER)
  2470. return 256 * (frame_bytes / 64);
  2471. if (id == AV_CODEC_ID_RA_144)
  2472. return 160 * (frame_bytes / 20);
  2473. if (id == AV_CODEC_ID_G723_1)
  2474. return 240 * (frame_bytes / 24);
  2475. if (bps > 0) {
  2476. /* calc from frame_bytes and bits_per_coded_sample */
  2477. if (id == AV_CODEC_ID_ADPCM_G726)
  2478. return frame_bytes * 8 / bps;
  2479. }
  2480. if (ch > 0) {
  2481. /* calc from frame_bytes and channels */
  2482. switch (id) {
  2483. case AV_CODEC_ID_ADPCM_AFC:
  2484. return frame_bytes / (9 * ch) * 16;
  2485. case AV_CODEC_ID_ADPCM_DTK:
  2486. return frame_bytes / (16 * ch) * 28;
  2487. case AV_CODEC_ID_ADPCM_4XM:
  2488. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2489. return (frame_bytes - 4 * ch) * 2 / ch;
  2490. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2491. return (frame_bytes - 4) * 2 / ch;
  2492. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2493. return (frame_bytes - 8) * 2 / ch;
  2494. case AV_CODEC_ID_ADPCM_XA:
  2495. return (frame_bytes / 128) * 224 / ch;
  2496. case AV_CODEC_ID_INTERPLAY_DPCM:
  2497. return (frame_bytes - 6 - ch) / ch;
  2498. case AV_CODEC_ID_ROQ_DPCM:
  2499. return (frame_bytes - 8) / ch;
  2500. case AV_CODEC_ID_XAN_DPCM:
  2501. return (frame_bytes - 2 * ch) / ch;
  2502. case AV_CODEC_ID_MACE3:
  2503. return 3 * frame_bytes / ch;
  2504. case AV_CODEC_ID_MACE6:
  2505. return 6 * frame_bytes / ch;
  2506. case AV_CODEC_ID_PCM_LXF:
  2507. return 2 * (frame_bytes / (5 * ch));
  2508. case AV_CODEC_ID_IAC:
  2509. case AV_CODEC_ID_IMC:
  2510. return 4 * frame_bytes / ch;
  2511. }
  2512. if (tag) {
  2513. /* calc from frame_bytes, channels, and codec_tag */
  2514. if (id == AV_CODEC_ID_SOL_DPCM) {
  2515. if (tag == 3)
  2516. return frame_bytes / ch;
  2517. else
  2518. return frame_bytes * 2 / ch;
  2519. }
  2520. }
  2521. if (ba > 0) {
  2522. /* calc from frame_bytes, channels, and block_align */
  2523. int blocks = frame_bytes / ba;
  2524. switch (avctx->codec_id) {
  2525. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2526. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2527. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2528. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2529. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2530. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2531. case AV_CODEC_ID_ADPCM_IMA_RAD:
  2532. return blocks * ((ba - 4 * ch) * 2 / ch);
  2533. case AV_CODEC_ID_ADPCM_MS:
  2534. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2535. }
  2536. }
  2537. if (bps > 0) {
  2538. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2539. switch (avctx->codec_id) {
  2540. case AV_CODEC_ID_PCM_DVD:
  2541. if(bps<4)
  2542. return 0;
  2543. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2544. case AV_CODEC_ID_PCM_BLURAY:
  2545. if(bps<4)
  2546. return 0;
  2547. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2548. case AV_CODEC_ID_S302M:
  2549. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2550. }
  2551. }
  2552. }
  2553. }
  2554. return 0;
  2555. }
  2556. #if !HAVE_THREADS
  2557. int ff_thread_init(AVCodecContext *s)
  2558. {
  2559. return -1;
  2560. }
  2561. #endif
  2562. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2563. {
  2564. unsigned int n = 0;
  2565. while (v >= 0xff) {
  2566. *s++ = 0xff;
  2567. v -= 0xff;
  2568. n++;
  2569. }
  2570. *s = v;
  2571. n++;
  2572. return n;
  2573. }
  2574. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2575. {
  2576. int i;
  2577. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2578. return i;
  2579. }
  2580. #if FF_API_MISSING_SAMPLE
  2581. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2582. {
  2583. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2584. "version to the newest one from Git. If the problem still "
  2585. "occurs, it means that your file has a feature which has not "
  2586. "been implemented.\n", feature);
  2587. if(want_sample)
  2588. av_log_ask_for_sample(avc, NULL);
  2589. }
  2590. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2591. {
  2592. va_list argument_list;
  2593. va_start(argument_list, msg);
  2594. if (msg)
  2595. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2596. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2597. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2598. "and contact the ffmpeg-devel mailing list.\n");
  2599. va_end(argument_list);
  2600. }
  2601. #endif /* FF_API_MISSING_SAMPLE */
  2602. static AVHWAccel *first_hwaccel = NULL;
  2603. void av_register_hwaccel(AVHWAccel *hwaccel)
  2604. {
  2605. AVHWAccel **p = &first_hwaccel;
  2606. while (*p)
  2607. p = &(*p)->next;
  2608. *p = hwaccel;
  2609. hwaccel->next = NULL;
  2610. }
  2611. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2612. {
  2613. return hwaccel ? hwaccel->next : first_hwaccel;
  2614. }
  2615. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  2616. {
  2617. AVHWAccel *hwaccel = NULL;
  2618. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2619. if (hwaccel->id == codec_id
  2620. && hwaccel->pix_fmt == pix_fmt)
  2621. return hwaccel;
  2622. return NULL;
  2623. }
  2624. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2625. {
  2626. if (lockmgr_cb) {
  2627. if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2628. return -1;
  2629. if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2630. return -1;
  2631. }
  2632. lockmgr_cb = cb;
  2633. if (lockmgr_cb) {
  2634. if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2635. return -1;
  2636. if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2637. return -1;
  2638. }
  2639. return 0;
  2640. }
  2641. int ff_lock_avcodec(AVCodecContext *log_ctx)
  2642. {
  2643. if (lockmgr_cb) {
  2644. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  2645. return -1;
  2646. }
  2647. entangled_thread_counter++;
  2648. if (entangled_thread_counter != 1) {
  2649. av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  2650. ff_avcodec_locked = 1;
  2651. ff_unlock_avcodec();
  2652. return AVERROR(EINVAL);
  2653. }
  2654. av_assert0(!ff_avcodec_locked);
  2655. ff_avcodec_locked = 1;
  2656. return 0;
  2657. }
  2658. int ff_unlock_avcodec(void)
  2659. {
  2660. av_assert0(ff_avcodec_locked);
  2661. ff_avcodec_locked = 0;
  2662. entangled_thread_counter--;
  2663. if (lockmgr_cb) {
  2664. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  2665. return -1;
  2666. }
  2667. return 0;
  2668. }
  2669. int avpriv_lock_avformat(void)
  2670. {
  2671. if (lockmgr_cb) {
  2672. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2673. return -1;
  2674. }
  2675. return 0;
  2676. }
  2677. int avpriv_unlock_avformat(void)
  2678. {
  2679. if (lockmgr_cb) {
  2680. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2681. return -1;
  2682. }
  2683. return 0;
  2684. }
  2685. unsigned int avpriv_toupper4(unsigned int x)
  2686. {
  2687. return av_toupper(x & 0xFF) +
  2688. (av_toupper((x >> 8) & 0xFF) << 8) +
  2689. (av_toupper((x >> 16) & 0xFF) << 16) +
  2690. (av_toupper((x >> 24) & 0xFF) << 24);
  2691. }
  2692. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  2693. {
  2694. int ret;
  2695. dst->owner = src->owner;
  2696. ret = av_frame_ref(dst->f, src->f);
  2697. if (ret < 0)
  2698. return ret;
  2699. if (src->progress &&
  2700. !(dst->progress = av_buffer_ref(src->progress))) {
  2701. ff_thread_release_buffer(dst->owner, dst);
  2702. return AVERROR(ENOMEM);
  2703. }
  2704. return 0;
  2705. }
  2706. #if !HAVE_THREADS
  2707. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  2708. {
  2709. return avctx->get_format(avctx, fmt);
  2710. }
  2711. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  2712. {
  2713. f->owner = avctx;
  2714. return ff_get_buffer(avctx, f->f, flags);
  2715. }
  2716. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  2717. {
  2718. av_frame_unref(f->f);
  2719. }
  2720. void ff_thread_finish_setup(AVCodecContext *avctx)
  2721. {
  2722. }
  2723. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  2724. {
  2725. }
  2726. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  2727. {
  2728. }
  2729. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2730. {
  2731. return 1;
  2732. }
  2733. #endif
  2734. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2735. {
  2736. AVCodec *c= avcodec_find_decoder(codec_id);
  2737. if(!c)
  2738. c= avcodec_find_encoder(codec_id);
  2739. if(c)
  2740. return c->type;
  2741. if (codec_id <= AV_CODEC_ID_NONE)
  2742. return AVMEDIA_TYPE_UNKNOWN;
  2743. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2744. return AVMEDIA_TYPE_VIDEO;
  2745. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2746. return AVMEDIA_TYPE_AUDIO;
  2747. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2748. return AVMEDIA_TYPE_SUBTITLE;
  2749. return AVMEDIA_TYPE_UNKNOWN;
  2750. }
  2751. int avcodec_is_open(AVCodecContext *s)
  2752. {
  2753. return !!s->internal;
  2754. }
  2755. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  2756. {
  2757. int ret;
  2758. char *str;
  2759. ret = av_bprint_finalize(buf, &str);
  2760. if (ret < 0)
  2761. return ret;
  2762. avctx->extradata = str;
  2763. /* Note: the string is NUL terminated (so extradata can be read as a
  2764. * string), but the ending character is not accounted in the size (in
  2765. * binary formats you are likely not supposed to mux that character). When
  2766. * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
  2767. * zeros. */
  2768. avctx->extradata_size = buf->len;
  2769. return 0;
  2770. }
  2771. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  2772. const uint8_t *end,
  2773. uint32_t *av_restrict state)
  2774. {
  2775. int i;
  2776. assert(p <= end);
  2777. if (p >= end)
  2778. return end;
  2779. for (i = 0; i < 3; i++) {
  2780. uint32_t tmp = *state << 8;
  2781. *state = tmp + *(p++);
  2782. if (tmp == 0x100 || p == end)
  2783. return p;
  2784. }
  2785. while (p < end) {
  2786. if (p[-1] > 1 ) p += 3;
  2787. else if (p[-2] ) p += 2;
  2788. else if (p[-3]|(p[-1]-1)) p++;
  2789. else {
  2790. p++;
  2791. break;
  2792. }
  2793. }
  2794. p = FFMIN(p, end) - 4;
  2795. *state = AV_RB32(p);
  2796. return p + 4;
  2797. }