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.

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