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.

3669 lines
120KB

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