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.

3096 lines
100KB

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