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.

3070 lines
99KB

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