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.

3064 lines
99KB

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