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.

3037 lines
98KB

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