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.

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