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.

3088 lines
100KB

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