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.

3218 lines
104KB

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