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.

3069 lines
99KB

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