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.

3199 lines
103KB

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