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.

3219 lines
104KB

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