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.

3194 lines
103KB

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