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.

3243 lines
105KB

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