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.

3275 lines
106KB

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