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.

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