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.

3665 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. return ret;
  911. }
  912. static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
  913. {
  914. AVFrame *tmp;
  915. int ret;
  916. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  917. if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
  918. av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  919. frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  920. av_frame_unref(frame);
  921. }
  922. ff_init_buffer_info(avctx, frame);
  923. if (!frame->data[0])
  924. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  925. if (av_frame_is_writable(frame))
  926. return ff_decode_frame_props(avctx, frame);
  927. tmp = av_frame_alloc();
  928. if (!tmp)
  929. return AVERROR(ENOMEM);
  930. av_frame_move_ref(tmp, frame);
  931. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  932. if (ret < 0) {
  933. av_frame_free(&tmp);
  934. return ret;
  935. }
  936. av_frame_copy(frame, tmp);
  937. av_frame_free(&tmp);
  938. return 0;
  939. }
  940. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  941. {
  942. int ret = reget_buffer_internal(avctx, frame);
  943. if (ret < 0)
  944. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  945. return ret;
  946. }
  947. #if FF_API_GET_BUFFER
  948. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  949. {
  950. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  951. av_frame_unref(pic);
  952. }
  953. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  954. {
  955. av_assert0(0);
  956. return AVERROR_BUG;
  957. }
  958. #endif
  959. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  960. {
  961. int i;
  962. for (i = 0; i < count; i++) {
  963. int r = func(c, (char *)arg + i * size);
  964. if (ret)
  965. ret[i] = r;
  966. }
  967. return 0;
  968. }
  969. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  970. {
  971. int i;
  972. for (i = 0; i < count; i++) {
  973. int r = func(c, arg, i, 0);
  974. if (ret)
  975. ret[i] = r;
  976. }
  977. return 0;
  978. }
  979. enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
  980. unsigned int fourcc)
  981. {
  982. while (tags->pix_fmt >= 0) {
  983. if (tags->fourcc == fourcc)
  984. return tags->pix_fmt;
  985. tags++;
  986. }
  987. return AV_PIX_FMT_NONE;
  988. }
  989. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  990. {
  991. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  992. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  993. }
  994. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  995. {
  996. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  997. ++fmt;
  998. return fmt[0];
  999. }
  1000. static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
  1001. enum AVPixelFormat pix_fmt)
  1002. {
  1003. AVHWAccel *hwaccel = NULL;
  1004. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1005. if (hwaccel->id == codec_id
  1006. && hwaccel->pix_fmt == pix_fmt)
  1007. return hwaccel;
  1008. return NULL;
  1009. }
  1010. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  1011. {
  1012. const AVPixFmtDescriptor *desc;
  1013. enum AVPixelFormat ret = avctx->get_format(avctx, fmt);
  1014. desc = av_pix_fmt_desc_get(ret);
  1015. if (!desc)
  1016. return AV_PIX_FMT_NONE;
  1017. if (avctx->hwaccel && avctx->hwaccel->uninit)
  1018. avctx->hwaccel->uninit(avctx);
  1019. av_freep(&avctx->internal->hwaccel_priv_data);
  1020. avctx->hwaccel = NULL;
  1021. if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL &&
  1022. !(avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)) {
  1023. AVHWAccel *hwaccel;
  1024. int err;
  1025. hwaccel = find_hwaccel(avctx->codec_id, ret);
  1026. if (!hwaccel) {
  1027. av_log(avctx, AV_LOG_ERROR,
  1028. "Could not find an AVHWAccel for the pixel format: %s",
  1029. desc->name);
  1030. return AV_PIX_FMT_NONE;
  1031. }
  1032. if (hwaccel->priv_data_size) {
  1033. avctx->internal->hwaccel_priv_data = av_mallocz(hwaccel->priv_data_size);
  1034. if (!avctx->internal->hwaccel_priv_data)
  1035. return AV_PIX_FMT_NONE;
  1036. }
  1037. if (hwaccel->init) {
  1038. err = hwaccel->init(avctx);
  1039. if (err < 0) {
  1040. av_freep(&avctx->internal->hwaccel_priv_data);
  1041. return AV_PIX_FMT_NONE;
  1042. }
  1043. }
  1044. avctx->hwaccel = hwaccel;
  1045. }
  1046. return ret;
  1047. }
  1048. #if FF_API_AVFRAME_LAVC
  1049. void avcodec_get_frame_defaults(AVFrame *frame)
  1050. {
  1051. #if LIBAVCODEC_VERSION_MAJOR >= 55
  1052. // extended_data should explicitly be freed when needed, this code is unsafe currently
  1053. // also this is not compatible to the <55 ABI/API
  1054. if (frame->extended_data != frame->data && 0)
  1055. av_freep(&frame->extended_data);
  1056. #endif
  1057. memset(frame, 0, sizeof(AVFrame));
  1058. av_frame_unref(frame);
  1059. }
  1060. AVFrame *avcodec_alloc_frame(void)
  1061. {
  1062. return av_frame_alloc();
  1063. }
  1064. void avcodec_free_frame(AVFrame **frame)
  1065. {
  1066. av_frame_free(frame);
  1067. }
  1068. #endif
  1069. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  1070. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  1071. MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
  1072. MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
  1073. MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
  1074. int av_codec_get_max_lowres(const AVCodec *codec)
  1075. {
  1076. return codec->max_lowres;
  1077. }
  1078. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  1079. {
  1080. memset(sub, 0, sizeof(*sub));
  1081. sub->pts = AV_NOPTS_VALUE;
  1082. }
  1083. static int get_bit_rate(AVCodecContext *ctx)
  1084. {
  1085. int bit_rate;
  1086. int bits_per_sample;
  1087. switch (ctx->codec_type) {
  1088. case AVMEDIA_TYPE_VIDEO:
  1089. case AVMEDIA_TYPE_DATA:
  1090. case AVMEDIA_TYPE_SUBTITLE:
  1091. case AVMEDIA_TYPE_ATTACHMENT:
  1092. bit_rate = ctx->bit_rate;
  1093. break;
  1094. case AVMEDIA_TYPE_AUDIO:
  1095. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1096. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1097. break;
  1098. default:
  1099. bit_rate = 0;
  1100. break;
  1101. }
  1102. return bit_rate;
  1103. }
  1104. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  1105. {
  1106. int ret = 0;
  1107. ff_unlock_avcodec();
  1108. ret = avcodec_open2(avctx, codec, options);
  1109. ff_lock_avcodec(avctx);
  1110. return ret;
  1111. }
  1112. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  1113. {
  1114. int ret = 0;
  1115. AVDictionary *tmp = NULL;
  1116. if (avcodec_is_open(avctx))
  1117. return 0;
  1118. if ((!codec && !avctx->codec)) {
  1119. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  1120. return AVERROR(EINVAL);
  1121. }
  1122. if ((codec && avctx->codec && codec != avctx->codec)) {
  1123. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  1124. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  1125. return AVERROR(EINVAL);
  1126. }
  1127. if (!codec)
  1128. codec = avctx->codec;
  1129. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  1130. return AVERROR(EINVAL);
  1131. if (options)
  1132. av_dict_copy(&tmp, *options, 0);
  1133. ret = ff_lock_avcodec(avctx);
  1134. if (ret < 0)
  1135. return ret;
  1136. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  1137. if (!avctx->internal) {
  1138. ret = AVERROR(ENOMEM);
  1139. goto end;
  1140. }
  1141. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  1142. if (!avctx->internal->pool) {
  1143. ret = AVERROR(ENOMEM);
  1144. goto free_and_end;
  1145. }
  1146. avctx->internal->to_free = av_frame_alloc();
  1147. if (!avctx->internal->to_free) {
  1148. ret = AVERROR(ENOMEM);
  1149. goto free_and_end;
  1150. }
  1151. if (codec->priv_data_size > 0) {
  1152. if (!avctx->priv_data) {
  1153. avctx->priv_data = av_mallocz(codec->priv_data_size);
  1154. if (!avctx->priv_data) {
  1155. ret = AVERROR(ENOMEM);
  1156. goto end;
  1157. }
  1158. if (codec->priv_class) {
  1159. *(const AVClass **)avctx->priv_data = codec->priv_class;
  1160. av_opt_set_defaults(avctx->priv_data);
  1161. }
  1162. }
  1163. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  1164. goto free_and_end;
  1165. } else {
  1166. avctx->priv_data = NULL;
  1167. }
  1168. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  1169. goto free_and_end;
  1170. // only call ff_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
  1171. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  1172. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
  1173. if (avctx->coded_width && avctx->coded_height)
  1174. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  1175. else if (avctx->width && avctx->height)
  1176. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1177. if (ret < 0)
  1178. goto free_and_end;
  1179. }
  1180. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  1181. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  1182. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  1183. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  1184. ff_set_dimensions(avctx, 0, 0);
  1185. }
  1186. if (avctx->width > 0 && avctx->height > 0) {
  1187. if (av_image_check_sar(avctx->width, avctx->height,
  1188. avctx->sample_aspect_ratio) < 0) {
  1189. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  1190. avctx->sample_aspect_ratio.num,
  1191. avctx->sample_aspect_ratio.den);
  1192. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  1193. }
  1194. }
  1195. /* if the decoder init function was already called previously,
  1196. * free the already allocated subtitle_header before overwriting it */
  1197. if (av_codec_is_decoder(codec))
  1198. av_freep(&avctx->subtitle_header);
  1199. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  1200. ret = AVERROR(EINVAL);
  1201. goto free_and_end;
  1202. }
  1203. avctx->codec = codec;
  1204. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  1205. avctx->codec_id == AV_CODEC_ID_NONE) {
  1206. avctx->codec_type = codec->type;
  1207. avctx->codec_id = codec->id;
  1208. }
  1209. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  1210. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  1211. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  1212. ret = AVERROR(EINVAL);
  1213. goto free_and_end;
  1214. }
  1215. avctx->frame_number = 0;
  1216. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  1217. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  1218. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  1219. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  1220. AVCodec *codec2;
  1221. av_log(avctx, AV_LOG_ERROR,
  1222. "The %s '%s' is experimental but experimental codecs are not enabled, "
  1223. "add '-strict %d' if you want to use it.\n",
  1224. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  1225. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  1226. if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
  1227. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  1228. codec_string, codec2->name);
  1229. ret = AVERROR_EXPERIMENTAL;
  1230. goto free_and_end;
  1231. }
  1232. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  1233. (!avctx->time_base.num || !avctx->time_base.den)) {
  1234. avctx->time_base.num = 1;
  1235. avctx->time_base.den = avctx->sample_rate;
  1236. }
  1237. if (!HAVE_THREADS)
  1238. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  1239. if (CONFIG_FRAME_THREAD_ENCODER) {
  1240. ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  1241. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  1242. ff_lock_avcodec(avctx);
  1243. if (ret < 0)
  1244. goto free_and_end;
  1245. }
  1246. if (HAVE_THREADS
  1247. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  1248. ret = ff_thread_init(avctx);
  1249. if (ret < 0) {
  1250. goto free_and_end;
  1251. }
  1252. }
  1253. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  1254. avctx->thread_count = 1;
  1255. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  1256. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  1257. avctx->codec->max_lowres);
  1258. ret = AVERROR(EINVAL);
  1259. goto free_and_end;
  1260. }
  1261. #if FF_API_VISMV
  1262. if (avctx->debug_mv)
  1263. av_log(avctx, AV_LOG_WARNING, "The 'vismv' option is deprecated, "
  1264. "see the codecview filter instead.\n");
  1265. #endif
  1266. if (av_codec_is_encoder(avctx->codec)) {
  1267. int i;
  1268. if (avctx->codec->sample_fmts) {
  1269. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  1270. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  1271. break;
  1272. if (avctx->channels == 1 &&
  1273. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  1274. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  1275. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  1276. break;
  1277. }
  1278. }
  1279. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  1280. char buf[128];
  1281. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  1282. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  1283. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  1284. ret = AVERROR(EINVAL);
  1285. goto free_and_end;
  1286. }
  1287. }
  1288. if (avctx->codec->pix_fmts) {
  1289. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  1290. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  1291. break;
  1292. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  1293. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  1294. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  1295. char buf[128];
  1296. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  1297. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  1298. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  1299. ret = AVERROR(EINVAL);
  1300. goto free_and_end;
  1301. }
  1302. }
  1303. if (avctx->codec->supported_samplerates) {
  1304. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1305. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1306. break;
  1307. if (avctx->codec->supported_samplerates[i] == 0) {
  1308. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1309. avctx->sample_rate);
  1310. ret = AVERROR(EINVAL);
  1311. goto free_and_end;
  1312. }
  1313. }
  1314. if (avctx->codec->channel_layouts) {
  1315. if (!avctx->channel_layout) {
  1316. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  1317. } else {
  1318. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1319. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1320. break;
  1321. if (avctx->codec->channel_layouts[i] == 0) {
  1322. char buf[512];
  1323. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1324. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  1325. ret = AVERROR(EINVAL);
  1326. goto free_and_end;
  1327. }
  1328. }
  1329. }
  1330. if (avctx->channel_layout && avctx->channels) {
  1331. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1332. if (channels != avctx->channels) {
  1333. char buf[512];
  1334. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1335. av_log(avctx, AV_LOG_ERROR,
  1336. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  1337. buf, channels, avctx->channels);
  1338. ret = AVERROR(EINVAL);
  1339. goto free_and_end;
  1340. }
  1341. } else if (avctx->channel_layout) {
  1342. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1343. }
  1344. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1345. if (avctx->width <= 0 || avctx->height <= 0) {
  1346. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  1347. ret = AVERROR(EINVAL);
  1348. goto free_and_end;
  1349. }
  1350. }
  1351. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  1352. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  1353. av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
  1354. }
  1355. if (!avctx->rc_initial_buffer_occupancy)
  1356. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1357. }
  1358. avctx->pts_correction_num_faulty_pts =
  1359. avctx->pts_correction_num_faulty_dts = 0;
  1360. avctx->pts_correction_last_pts =
  1361. avctx->pts_correction_last_dts = INT64_MIN;
  1362. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  1363. || avctx->internal->frame_thread_encoder)) {
  1364. ret = avctx->codec->init(avctx);
  1365. if (ret < 0) {
  1366. goto free_and_end;
  1367. }
  1368. }
  1369. ret=0;
  1370. if (av_codec_is_decoder(avctx->codec)) {
  1371. if (!avctx->bit_rate)
  1372. avctx->bit_rate = get_bit_rate(avctx);
  1373. /* validate channel layout from the decoder */
  1374. if (avctx->channel_layout) {
  1375. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1376. if (!avctx->channels)
  1377. avctx->channels = channels;
  1378. else if (channels != avctx->channels) {
  1379. char buf[512];
  1380. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1381. av_log(avctx, AV_LOG_WARNING,
  1382. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  1383. "ignoring specified channel layout\n",
  1384. buf, channels, avctx->channels);
  1385. avctx->channel_layout = 0;
  1386. }
  1387. }
  1388. if (avctx->channels && avctx->channels < 0 ||
  1389. avctx->channels > FF_SANE_NB_CHANNELS) {
  1390. ret = AVERROR(EINVAL);
  1391. goto free_and_end;
  1392. }
  1393. if (avctx->sub_charenc) {
  1394. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1395. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1396. "supported with subtitles codecs\n");
  1397. ret = AVERROR(EINVAL);
  1398. goto free_and_end;
  1399. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1400. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1401. "subtitles character encoding will be ignored\n",
  1402. avctx->codec_descriptor->name);
  1403. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1404. } else {
  1405. /* input character encoding is set for a text based subtitle
  1406. * codec at this point */
  1407. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1408. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1409. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1410. #if CONFIG_ICONV
  1411. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1412. if (cd == (iconv_t)-1) {
  1413. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1414. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1415. ret = AVERROR(errno);
  1416. goto free_and_end;
  1417. }
  1418. iconv_close(cd);
  1419. #else
  1420. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1421. "conversion needs a libavcodec built with iconv support "
  1422. "for this codec\n");
  1423. ret = AVERROR(ENOSYS);
  1424. goto free_and_end;
  1425. #endif
  1426. }
  1427. }
  1428. }
  1429. }
  1430. end:
  1431. ff_unlock_avcodec();
  1432. if (options) {
  1433. av_dict_free(options);
  1434. *options = tmp;
  1435. }
  1436. return ret;
  1437. free_and_end:
  1438. av_dict_free(&tmp);
  1439. av_freep(&avctx->priv_data);
  1440. if (avctx->internal) {
  1441. av_frame_free(&avctx->internal->to_free);
  1442. av_freep(&avctx->internal->pool);
  1443. }
  1444. av_freep(&avctx->internal);
  1445. avctx->codec = NULL;
  1446. goto end;
  1447. }
  1448. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
  1449. {
  1450. if (avpkt->size < 0) {
  1451. av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
  1452. return AVERROR(EINVAL);
  1453. }
  1454. if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  1455. av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
  1456. size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
  1457. return AVERROR(EINVAL);
  1458. }
  1459. if (avctx) {
  1460. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1461. if (!avpkt->data || avpkt->size < size) {
  1462. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1463. avpkt->data = avctx->internal->byte_buffer;
  1464. avpkt->size = avctx->internal->byte_buffer_size;
  1465. #if FF_API_DESTRUCT_PACKET
  1466. FF_DISABLE_DEPRECATION_WARNINGS
  1467. avpkt->destruct = NULL;
  1468. FF_ENABLE_DEPRECATION_WARNINGS
  1469. #endif
  1470. }
  1471. }
  1472. if (avpkt->data) {
  1473. AVBufferRef *buf = avpkt->buf;
  1474. #if FF_API_DESTRUCT_PACKET
  1475. FF_DISABLE_DEPRECATION_WARNINGS
  1476. void *destruct = avpkt->destruct;
  1477. FF_ENABLE_DEPRECATION_WARNINGS
  1478. #endif
  1479. if (avpkt->size < size) {
  1480. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
  1481. return AVERROR(EINVAL);
  1482. }
  1483. av_init_packet(avpkt);
  1484. #if FF_API_DESTRUCT_PACKET
  1485. FF_DISABLE_DEPRECATION_WARNINGS
  1486. avpkt->destruct = destruct;
  1487. FF_ENABLE_DEPRECATION_WARNINGS
  1488. #endif
  1489. avpkt->buf = buf;
  1490. avpkt->size = size;
  1491. return 0;
  1492. } else {
  1493. int ret = av_new_packet(avpkt, size);
  1494. if (ret < 0)
  1495. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
  1496. return ret;
  1497. }
  1498. }
  1499. int ff_alloc_packet(AVPacket *avpkt, int size)
  1500. {
  1501. return ff_alloc_packet2(NULL, avpkt, size);
  1502. }
  1503. /**
  1504. * Pad last frame with silence.
  1505. */
  1506. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1507. {
  1508. AVFrame *frame = NULL;
  1509. int ret;
  1510. if (!(frame = av_frame_alloc()))
  1511. return AVERROR(ENOMEM);
  1512. frame->format = src->format;
  1513. frame->channel_layout = src->channel_layout;
  1514. av_frame_set_channels(frame, av_frame_get_channels(src));
  1515. frame->nb_samples = s->frame_size;
  1516. ret = av_frame_get_buffer(frame, 32);
  1517. if (ret < 0)
  1518. goto fail;
  1519. ret = av_frame_copy_props(frame, src);
  1520. if (ret < 0)
  1521. goto fail;
  1522. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1523. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1524. goto fail;
  1525. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1526. frame->nb_samples - src->nb_samples,
  1527. s->channels, s->sample_fmt)) < 0)
  1528. goto fail;
  1529. *dst = frame;
  1530. return 0;
  1531. fail:
  1532. av_frame_free(&frame);
  1533. return ret;
  1534. }
  1535. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1536. AVPacket *avpkt,
  1537. const AVFrame *frame,
  1538. int *got_packet_ptr)
  1539. {
  1540. AVFrame *extended_frame = NULL;
  1541. AVFrame *padded_frame = NULL;
  1542. int ret;
  1543. AVPacket user_pkt = *avpkt;
  1544. int needs_realloc = !user_pkt.data;
  1545. *got_packet_ptr = 0;
  1546. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1547. av_free_packet(avpkt);
  1548. av_init_packet(avpkt);
  1549. return 0;
  1550. }
  1551. /* ensure that extended_data is properly set */
  1552. if (frame && !frame->extended_data) {
  1553. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1554. avctx->channels > AV_NUM_DATA_POINTERS) {
  1555. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1556. "with more than %d channels, but extended_data is not set.\n",
  1557. AV_NUM_DATA_POINTERS);
  1558. return AVERROR(EINVAL);
  1559. }
  1560. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1561. extended_frame = av_frame_alloc();
  1562. if (!extended_frame)
  1563. return AVERROR(ENOMEM);
  1564. memcpy(extended_frame, frame, sizeof(AVFrame));
  1565. extended_frame->extended_data = extended_frame->data;
  1566. frame = extended_frame;
  1567. }
  1568. /* check for valid frame size */
  1569. if (frame) {
  1570. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1571. if (frame->nb_samples > avctx->frame_size) {
  1572. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1573. ret = AVERROR(EINVAL);
  1574. goto end;
  1575. }
  1576. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1577. if (frame->nb_samples < avctx->frame_size &&
  1578. !avctx->internal->last_audio_frame) {
  1579. ret = pad_last_frame(avctx, &padded_frame, frame);
  1580. if (ret < 0)
  1581. goto end;
  1582. frame = padded_frame;
  1583. avctx->internal->last_audio_frame = 1;
  1584. }
  1585. if (frame->nb_samples != avctx->frame_size) {
  1586. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1587. ret = AVERROR(EINVAL);
  1588. goto end;
  1589. }
  1590. }
  1591. }
  1592. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1593. if (!ret) {
  1594. if (*got_packet_ptr) {
  1595. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1596. if (avpkt->pts == AV_NOPTS_VALUE)
  1597. avpkt->pts = frame->pts;
  1598. if (!avpkt->duration)
  1599. avpkt->duration = ff_samples_to_time_base(avctx,
  1600. frame->nb_samples);
  1601. }
  1602. avpkt->dts = avpkt->pts;
  1603. } else {
  1604. avpkt->size = 0;
  1605. }
  1606. }
  1607. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1608. needs_realloc = 0;
  1609. if (user_pkt.data) {
  1610. if (user_pkt.size >= avpkt->size) {
  1611. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1612. } else {
  1613. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1614. avpkt->size = user_pkt.size;
  1615. ret = -1;
  1616. }
  1617. avpkt->buf = user_pkt.buf;
  1618. avpkt->data = user_pkt.data;
  1619. #if FF_API_DESTRUCT_PACKET
  1620. FF_DISABLE_DEPRECATION_WARNINGS
  1621. avpkt->destruct = user_pkt.destruct;
  1622. FF_ENABLE_DEPRECATION_WARNINGS
  1623. #endif
  1624. } else {
  1625. if (av_dup_packet(avpkt) < 0) {
  1626. ret = AVERROR(ENOMEM);
  1627. }
  1628. }
  1629. }
  1630. if (!ret) {
  1631. if (needs_realloc && avpkt->data) {
  1632. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1633. if (ret >= 0)
  1634. avpkt->data = avpkt->buf->data;
  1635. }
  1636. avctx->frame_number++;
  1637. }
  1638. if (ret < 0 || !*got_packet_ptr) {
  1639. av_free_packet(avpkt);
  1640. av_init_packet(avpkt);
  1641. goto end;
  1642. }
  1643. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1644. * this needs to be moved to the encoders, but for now we can do it
  1645. * here to simplify things */
  1646. avpkt->flags |= AV_PKT_FLAG_KEY;
  1647. end:
  1648. av_frame_free(&padded_frame);
  1649. av_free(extended_frame);
  1650. return ret;
  1651. }
  1652. #if FF_API_OLD_ENCODE_AUDIO
  1653. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1654. uint8_t *buf, int buf_size,
  1655. const short *samples)
  1656. {
  1657. AVPacket pkt;
  1658. AVFrame *frame;
  1659. int ret, samples_size, got_packet;
  1660. av_init_packet(&pkt);
  1661. pkt.data = buf;
  1662. pkt.size = buf_size;
  1663. if (samples) {
  1664. frame = av_frame_alloc();
  1665. if (!frame)
  1666. return AVERROR(ENOMEM);
  1667. if (avctx->frame_size) {
  1668. frame->nb_samples = avctx->frame_size;
  1669. } else {
  1670. /* if frame_size is not set, the number of samples must be
  1671. * calculated from the buffer size */
  1672. int64_t nb_samples;
  1673. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1674. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1675. "support this codec\n");
  1676. av_frame_free(&frame);
  1677. return AVERROR(EINVAL);
  1678. }
  1679. nb_samples = (int64_t)buf_size * 8 /
  1680. (av_get_bits_per_sample(avctx->codec_id) *
  1681. avctx->channels);
  1682. if (nb_samples >= INT_MAX) {
  1683. av_frame_free(&frame);
  1684. return AVERROR(EINVAL);
  1685. }
  1686. frame->nb_samples = nb_samples;
  1687. }
  1688. /* it is assumed that the samples buffer is large enough based on the
  1689. * relevant parameters */
  1690. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1691. frame->nb_samples,
  1692. avctx->sample_fmt, 1);
  1693. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1694. avctx->sample_fmt,
  1695. (const uint8_t *)samples,
  1696. samples_size, 1)) < 0) {
  1697. av_frame_free(&frame);
  1698. return ret;
  1699. }
  1700. /* fabricate frame pts from sample count.
  1701. * this is needed because the avcodec_encode_audio() API does not have
  1702. * a way for the user to provide pts */
  1703. if (avctx->sample_rate && avctx->time_base.num)
  1704. frame->pts = ff_samples_to_time_base(avctx,
  1705. avctx->internal->sample_count);
  1706. else
  1707. frame->pts = AV_NOPTS_VALUE;
  1708. avctx->internal->sample_count += frame->nb_samples;
  1709. } else {
  1710. frame = NULL;
  1711. }
  1712. got_packet = 0;
  1713. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1714. if (!ret && got_packet && avctx->coded_frame) {
  1715. avctx->coded_frame->pts = pkt.pts;
  1716. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1717. }
  1718. /* free any side data since we cannot return it */
  1719. av_packet_free_side_data(&pkt);
  1720. if (frame && frame->extended_data != frame->data)
  1721. av_freep(&frame->extended_data);
  1722. av_frame_free(&frame);
  1723. return ret ? ret : pkt.size;
  1724. }
  1725. #endif
  1726. #if FF_API_OLD_ENCODE_VIDEO
  1727. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1728. const AVFrame *pict)
  1729. {
  1730. AVPacket pkt;
  1731. int ret, got_packet = 0;
  1732. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1733. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1734. return -1;
  1735. }
  1736. av_init_packet(&pkt);
  1737. pkt.data = buf;
  1738. pkt.size = buf_size;
  1739. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1740. if (!ret && got_packet && avctx->coded_frame) {
  1741. avctx->coded_frame->pts = pkt.pts;
  1742. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1743. }
  1744. /* free any side data since we cannot return it */
  1745. if (pkt.side_data_elems > 0) {
  1746. int i;
  1747. for (i = 0; i < pkt.side_data_elems; i++)
  1748. av_free(pkt.side_data[i].data);
  1749. av_freep(&pkt.side_data);
  1750. pkt.side_data_elems = 0;
  1751. }
  1752. return ret ? ret : pkt.size;
  1753. }
  1754. #endif
  1755. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1756. AVPacket *avpkt,
  1757. const AVFrame *frame,
  1758. int *got_packet_ptr)
  1759. {
  1760. int ret;
  1761. AVPacket user_pkt = *avpkt;
  1762. int needs_realloc = !user_pkt.data;
  1763. *got_packet_ptr = 0;
  1764. if(CONFIG_FRAME_THREAD_ENCODER &&
  1765. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1766. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1767. if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
  1768. avctx->stats_out[0] = '\0';
  1769. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1770. av_free_packet(avpkt);
  1771. av_init_packet(avpkt);
  1772. avpkt->size = 0;
  1773. return 0;
  1774. }
  1775. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1776. return AVERROR(EINVAL);
  1777. av_assert0(avctx->codec->encode2);
  1778. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1779. av_assert0(ret <= 0);
  1780. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1781. needs_realloc = 0;
  1782. if (user_pkt.data) {
  1783. if (user_pkt.size >= avpkt->size) {
  1784. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1785. } else {
  1786. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1787. avpkt->size = user_pkt.size;
  1788. ret = -1;
  1789. }
  1790. avpkt->buf = user_pkt.buf;
  1791. avpkt->data = user_pkt.data;
  1792. #if FF_API_DESTRUCT_PACKET
  1793. FF_DISABLE_DEPRECATION_WARNINGS
  1794. avpkt->destruct = user_pkt.destruct;
  1795. FF_ENABLE_DEPRECATION_WARNINGS
  1796. #endif
  1797. } else {
  1798. if (av_dup_packet(avpkt) < 0) {
  1799. ret = AVERROR(ENOMEM);
  1800. }
  1801. }
  1802. }
  1803. if (!ret) {
  1804. if (!*got_packet_ptr)
  1805. avpkt->size = 0;
  1806. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1807. avpkt->pts = avpkt->dts = frame->pts;
  1808. if (needs_realloc && avpkt->data) {
  1809. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1810. if (ret >= 0)
  1811. avpkt->data = avpkt->buf->data;
  1812. }
  1813. avctx->frame_number++;
  1814. }
  1815. if (ret < 0 || !*got_packet_ptr)
  1816. av_free_packet(avpkt);
  1817. else
  1818. av_packet_merge_side_data(avpkt);
  1819. emms_c();
  1820. return ret;
  1821. }
  1822. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1823. const AVSubtitle *sub)
  1824. {
  1825. int ret;
  1826. if (sub->start_display_time) {
  1827. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1828. return -1;
  1829. }
  1830. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1831. avctx->frame_number++;
  1832. return ret;
  1833. }
  1834. /**
  1835. * Attempt to guess proper monotonic timestamps for decoded video frames
  1836. * which might have incorrect times. Input timestamps may wrap around, in
  1837. * which case the output will as well.
  1838. *
  1839. * @param pts the pts field of the decoded AVPacket, as passed through
  1840. * AVFrame.pkt_pts
  1841. * @param dts the dts field of the decoded AVPacket
  1842. * @return one of the input values, may be AV_NOPTS_VALUE
  1843. */
  1844. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1845. int64_t reordered_pts, int64_t dts)
  1846. {
  1847. int64_t pts = AV_NOPTS_VALUE;
  1848. if (dts != AV_NOPTS_VALUE) {
  1849. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1850. ctx->pts_correction_last_dts = dts;
  1851. } else if (reordered_pts != AV_NOPTS_VALUE)
  1852. ctx->pts_correction_last_dts = reordered_pts;
  1853. if (reordered_pts != AV_NOPTS_VALUE) {
  1854. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1855. ctx->pts_correction_last_pts = reordered_pts;
  1856. } else if(dts != AV_NOPTS_VALUE)
  1857. ctx->pts_correction_last_pts = dts;
  1858. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1859. && reordered_pts != AV_NOPTS_VALUE)
  1860. pts = reordered_pts;
  1861. else
  1862. pts = dts;
  1863. return pts;
  1864. }
  1865. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1866. {
  1867. int size = 0, ret;
  1868. const uint8_t *data;
  1869. uint32_t flags;
  1870. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1871. if (!data)
  1872. return 0;
  1873. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
  1874. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  1875. "changes, but PARAM_CHANGE side data was sent to it.\n");
  1876. return AVERROR(EINVAL);
  1877. }
  1878. if (size < 4)
  1879. goto fail;
  1880. flags = bytestream_get_le32(&data);
  1881. size -= 4;
  1882. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1883. if (size < 4)
  1884. goto fail;
  1885. avctx->channels = bytestream_get_le32(&data);
  1886. size -= 4;
  1887. }
  1888. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1889. if (size < 8)
  1890. goto fail;
  1891. avctx->channel_layout = bytestream_get_le64(&data);
  1892. size -= 8;
  1893. }
  1894. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1895. if (size < 4)
  1896. goto fail;
  1897. avctx->sample_rate = bytestream_get_le32(&data);
  1898. size -= 4;
  1899. }
  1900. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1901. if (size < 8)
  1902. goto fail;
  1903. avctx->width = bytestream_get_le32(&data);
  1904. avctx->height = bytestream_get_le32(&data);
  1905. size -= 8;
  1906. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1907. if (ret < 0)
  1908. return ret;
  1909. }
  1910. return 0;
  1911. fail:
  1912. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  1913. return AVERROR_INVALIDDATA;
  1914. }
  1915. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  1916. {
  1917. int size;
  1918. const uint8_t *side_metadata;
  1919. AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
  1920. side_metadata = av_packet_get_side_data(avctx->internal->pkt,
  1921. AV_PKT_DATA_STRINGS_METADATA, &size);
  1922. return av_packet_unpack_dictionary(side_metadata, size, frame_md);
  1923. }
  1924. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  1925. {
  1926. int ret;
  1927. /* move the original frame to our backup */
  1928. av_frame_unref(avci->to_free);
  1929. av_frame_move_ref(avci->to_free, frame);
  1930. /* now copy everything except the AVBufferRefs back
  1931. * note that we make a COPY of the side data, so calling av_frame_free() on
  1932. * the caller's frame will work properly */
  1933. ret = av_frame_copy_props(frame, avci->to_free);
  1934. if (ret < 0)
  1935. return ret;
  1936. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  1937. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  1938. if (avci->to_free->extended_data != avci->to_free->data) {
  1939. int planes = av_frame_get_channels(avci->to_free);
  1940. int size = planes * sizeof(*frame->extended_data);
  1941. if (!size) {
  1942. av_frame_unref(frame);
  1943. return AVERROR_BUG;
  1944. }
  1945. frame->extended_data = av_malloc(size);
  1946. if (!frame->extended_data) {
  1947. av_frame_unref(frame);
  1948. return AVERROR(ENOMEM);
  1949. }
  1950. memcpy(frame->extended_data, avci->to_free->extended_data,
  1951. size);
  1952. } else
  1953. frame->extended_data = frame->data;
  1954. frame->format = avci->to_free->format;
  1955. frame->width = avci->to_free->width;
  1956. frame->height = avci->to_free->height;
  1957. frame->channel_layout = avci->to_free->channel_layout;
  1958. frame->nb_samples = avci->to_free->nb_samples;
  1959. av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
  1960. return 0;
  1961. }
  1962. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1963. int *got_picture_ptr,
  1964. const AVPacket *avpkt)
  1965. {
  1966. AVCodecInternal *avci = avctx->internal;
  1967. int ret;
  1968. // copy to ensure we do not change avpkt
  1969. AVPacket tmp = *avpkt;
  1970. if (!avctx->codec)
  1971. return AVERROR(EINVAL);
  1972. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1973. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1974. return AVERROR(EINVAL);
  1975. }
  1976. *got_picture_ptr = 0;
  1977. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1978. return AVERROR(EINVAL);
  1979. av_frame_unref(picture);
  1980. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1981. int did_split = av_packet_split_side_data(&tmp);
  1982. ret = apply_param_change(avctx, &tmp);
  1983. if (ret < 0) {
  1984. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1985. if (avctx->err_recognition & AV_EF_EXPLODE)
  1986. goto fail;
  1987. }
  1988. avctx->internal->pkt = &tmp;
  1989. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1990. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1991. &tmp);
  1992. else {
  1993. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1994. &tmp);
  1995. picture->pkt_dts = avpkt->dts;
  1996. if(!avctx->has_b_frames){
  1997. av_frame_set_pkt_pos(picture, avpkt->pos);
  1998. }
  1999. //FIXME these should be under if(!avctx->has_b_frames)
  2000. /* get_buffer is supposed to set frame parameters */
  2001. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  2002. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  2003. if (!picture->width) picture->width = avctx->width;
  2004. if (!picture->height) picture->height = avctx->height;
  2005. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  2006. }
  2007. }
  2008. add_metadata_from_side_data(avctx, picture);
  2009. fail:
  2010. emms_c(); //needed to avoid an emms_c() call before every return;
  2011. avctx->internal->pkt = NULL;
  2012. if (did_split) {
  2013. av_packet_free_side_data(&tmp);
  2014. if(ret == tmp.size)
  2015. ret = avpkt->size;
  2016. }
  2017. if (*got_picture_ptr) {
  2018. if (!avctx->refcounted_frames) {
  2019. int err = unrefcount_frame(avci, picture);
  2020. if (err < 0)
  2021. return err;
  2022. }
  2023. avctx->frame_number++;
  2024. av_frame_set_best_effort_timestamp(picture,
  2025. guess_correct_pts(avctx,
  2026. picture->pkt_pts,
  2027. picture->pkt_dts));
  2028. } else
  2029. av_frame_unref(picture);
  2030. } else
  2031. ret = 0;
  2032. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  2033. * make sure it's set correctly */
  2034. av_assert0(!picture->extended_data || picture->extended_data == picture->data);
  2035. return ret;
  2036. }
  2037. #if FF_API_OLD_DECODE_AUDIO
  2038. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  2039. int *frame_size_ptr,
  2040. AVPacket *avpkt)
  2041. {
  2042. AVFrame *frame = av_frame_alloc();
  2043. int ret, got_frame = 0;
  2044. if (!frame)
  2045. return AVERROR(ENOMEM);
  2046. if (avctx->get_buffer != avcodec_default_get_buffer) {
  2047. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  2048. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  2049. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  2050. "avcodec_decode_audio4()\n");
  2051. avctx->get_buffer = avcodec_default_get_buffer;
  2052. avctx->release_buffer = avcodec_default_release_buffer;
  2053. }
  2054. ret = avcodec_decode_audio4(avctx, frame, &got_frame, avpkt);
  2055. if (ret >= 0 && got_frame) {
  2056. int ch, plane_size;
  2057. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  2058. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  2059. frame->nb_samples,
  2060. avctx->sample_fmt, 1);
  2061. if (*frame_size_ptr < data_size) {
  2062. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  2063. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  2064. av_frame_free(&frame);
  2065. return AVERROR(EINVAL);
  2066. }
  2067. memcpy(samples, frame->extended_data[0], plane_size);
  2068. if (planar && avctx->channels > 1) {
  2069. uint8_t *out = ((uint8_t *)samples) + plane_size;
  2070. for (ch = 1; ch < avctx->channels; ch++) {
  2071. memcpy(out, frame->extended_data[ch], plane_size);
  2072. out += plane_size;
  2073. }
  2074. }
  2075. *frame_size_ptr = data_size;
  2076. } else {
  2077. *frame_size_ptr = 0;
  2078. }
  2079. av_frame_free(&frame);
  2080. return ret;
  2081. }
  2082. #endif
  2083. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  2084. AVFrame *frame,
  2085. int *got_frame_ptr,
  2086. const AVPacket *avpkt)
  2087. {
  2088. AVCodecInternal *avci = avctx->internal;
  2089. int ret = 0;
  2090. *got_frame_ptr = 0;
  2091. if (!avpkt->data && avpkt->size) {
  2092. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2093. return AVERROR(EINVAL);
  2094. }
  2095. if (!avctx->codec)
  2096. return AVERROR(EINVAL);
  2097. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  2098. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  2099. return AVERROR(EINVAL);
  2100. }
  2101. av_frame_unref(frame);
  2102. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  2103. uint8_t *side;
  2104. int side_size;
  2105. uint32_t discard_padding = 0;
  2106. // copy to ensure we do not change avpkt
  2107. AVPacket tmp = *avpkt;
  2108. int did_split = av_packet_split_side_data(&tmp);
  2109. ret = apply_param_change(avctx, &tmp);
  2110. if (ret < 0) {
  2111. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  2112. if (avctx->err_recognition & AV_EF_EXPLODE)
  2113. goto fail;
  2114. }
  2115. avctx->internal->pkt = &tmp;
  2116. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2117. ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
  2118. else {
  2119. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  2120. frame->pkt_dts = avpkt->dts;
  2121. }
  2122. if (ret >= 0 && *got_frame_ptr) {
  2123. add_metadata_from_side_data(avctx, frame);
  2124. avctx->frame_number++;
  2125. av_frame_set_best_effort_timestamp(frame,
  2126. guess_correct_pts(avctx,
  2127. frame->pkt_pts,
  2128. frame->pkt_dts));
  2129. if (frame->format == AV_SAMPLE_FMT_NONE)
  2130. frame->format = avctx->sample_fmt;
  2131. if (!frame->channel_layout)
  2132. frame->channel_layout = avctx->channel_layout;
  2133. if (!av_frame_get_channels(frame))
  2134. av_frame_set_channels(frame, avctx->channels);
  2135. if (!frame->sample_rate)
  2136. frame->sample_rate = avctx->sample_rate;
  2137. }
  2138. side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  2139. if(side && side_size>=10) {
  2140. avctx->internal->skip_samples = AV_RL32(side);
  2141. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  2142. avctx->internal->skip_samples);
  2143. discard_padding = AV_RL32(side + 4);
  2144. }
  2145. if (avctx->internal->skip_samples && *got_frame_ptr) {
  2146. if(frame->nb_samples <= avctx->internal->skip_samples){
  2147. *got_frame_ptr = 0;
  2148. avctx->internal->skip_samples -= frame->nb_samples;
  2149. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  2150. avctx->internal->skip_samples);
  2151. } else {
  2152. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  2153. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  2154. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  2155. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  2156. (AVRational){1, avctx->sample_rate},
  2157. avctx->pkt_timebase);
  2158. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  2159. frame->pkt_pts += diff_ts;
  2160. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  2161. frame->pkt_dts += diff_ts;
  2162. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  2163. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  2164. } else {
  2165. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  2166. }
  2167. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  2168. avctx->internal->skip_samples, frame->nb_samples);
  2169. frame->nb_samples -= avctx->internal->skip_samples;
  2170. avctx->internal->skip_samples = 0;
  2171. }
  2172. }
  2173. if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr) {
  2174. if (discard_padding == frame->nb_samples) {
  2175. *got_frame_ptr = 0;
  2176. } else {
  2177. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  2178. int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
  2179. (AVRational){1, avctx->sample_rate},
  2180. avctx->pkt_timebase);
  2181. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  2182. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  2183. } else {
  2184. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
  2185. }
  2186. av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
  2187. discard_padding, frame->nb_samples);
  2188. frame->nb_samples -= discard_padding;
  2189. }
  2190. }
  2191. fail:
  2192. avctx->internal->pkt = NULL;
  2193. if (did_split) {
  2194. av_packet_free_side_data(&tmp);
  2195. if(ret == tmp.size)
  2196. ret = avpkt->size;
  2197. }
  2198. if (ret >= 0 && *got_frame_ptr) {
  2199. if (!avctx->refcounted_frames) {
  2200. int err = unrefcount_frame(avci, frame);
  2201. if (err < 0)
  2202. return err;
  2203. }
  2204. } else
  2205. av_frame_unref(frame);
  2206. }
  2207. return ret;
  2208. }
  2209. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  2210. static int recode_subtitle(AVCodecContext *avctx,
  2211. AVPacket *outpkt, const AVPacket *inpkt)
  2212. {
  2213. #if CONFIG_ICONV
  2214. iconv_t cd = (iconv_t)-1;
  2215. int ret = 0;
  2216. char *inb, *outb;
  2217. size_t inl, outl;
  2218. AVPacket tmp;
  2219. #endif
  2220. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
  2221. return 0;
  2222. #if CONFIG_ICONV
  2223. cd = iconv_open("UTF-8", avctx->sub_charenc);
  2224. av_assert0(cd != (iconv_t)-1);
  2225. inb = inpkt->data;
  2226. inl = inpkt->size;
  2227. if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
  2228. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  2229. ret = AVERROR(ENOMEM);
  2230. goto end;
  2231. }
  2232. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  2233. if (ret < 0)
  2234. goto end;
  2235. outpkt->buf = tmp.buf;
  2236. outpkt->data = tmp.data;
  2237. outpkt->size = tmp.size;
  2238. outb = outpkt->data;
  2239. outl = outpkt->size;
  2240. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  2241. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  2242. outl >= outpkt->size || inl != 0) {
  2243. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  2244. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  2245. av_free_packet(&tmp);
  2246. ret = AVERROR(errno);
  2247. goto end;
  2248. }
  2249. outpkt->size -= outl;
  2250. memset(outpkt->data + outpkt->size, 0, outl);
  2251. end:
  2252. if (cd != (iconv_t)-1)
  2253. iconv_close(cd);
  2254. return ret;
  2255. #else
  2256. av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
  2257. return AVERROR(EINVAL);
  2258. #endif
  2259. }
  2260. static int utf8_check(const uint8_t *str)
  2261. {
  2262. const uint8_t *byte;
  2263. uint32_t codepoint, min;
  2264. while (*str) {
  2265. byte = str;
  2266. GET_UTF8(codepoint, *(byte++), return 0;);
  2267. min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  2268. 1 << (5 * (byte - str) - 4);
  2269. if (codepoint < min || codepoint >= 0x110000 ||
  2270. codepoint == 0xFFFE /* BOM */ ||
  2271. codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  2272. return 0;
  2273. str = byte;
  2274. }
  2275. return 1;
  2276. }
  2277. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  2278. int *got_sub_ptr,
  2279. AVPacket *avpkt)
  2280. {
  2281. int i, ret = 0;
  2282. if (!avpkt->data && avpkt->size) {
  2283. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2284. return AVERROR(EINVAL);
  2285. }
  2286. if (!avctx->codec)
  2287. return AVERROR(EINVAL);
  2288. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  2289. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  2290. return AVERROR(EINVAL);
  2291. }
  2292. *got_sub_ptr = 0;
  2293. avcodec_get_subtitle_defaults(sub);
  2294. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  2295. AVPacket pkt_recoded;
  2296. AVPacket tmp = *avpkt;
  2297. int did_split = av_packet_split_side_data(&tmp);
  2298. //apply_param_change(avctx, &tmp);
  2299. if (did_split) {
  2300. /* FFMIN() prevents overflow in case the packet wasn't allocated with
  2301. * proper padding.
  2302. * If the side data is smaller than the buffer padding size, the
  2303. * remaining bytes should have already been filled with zeros by the
  2304. * original packet allocation anyway. */
  2305. memset(tmp.data + tmp.size, 0,
  2306. FFMIN(avpkt->size - tmp.size, FF_INPUT_BUFFER_PADDING_SIZE));
  2307. }
  2308. pkt_recoded = tmp;
  2309. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  2310. if (ret < 0) {
  2311. *got_sub_ptr = 0;
  2312. } else {
  2313. avctx->internal->pkt = &pkt_recoded;
  2314. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  2315. sub->pts = av_rescale_q(avpkt->pts,
  2316. avctx->pkt_timebase, AV_TIME_BASE_Q);
  2317. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  2318. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  2319. !!*got_sub_ptr >= !!sub->num_rects);
  2320. if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  2321. avctx->pkt_timebase.num) {
  2322. AVRational ms = { 1, 1000 };
  2323. sub->end_display_time = av_rescale_q(avpkt->duration,
  2324. avctx->pkt_timebase, ms);
  2325. }
  2326. for (i = 0; i < sub->num_rects; i++) {
  2327. if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  2328. av_log(avctx, AV_LOG_ERROR,
  2329. "Invalid UTF-8 in decoded subtitles text; "
  2330. "maybe missing -sub_charenc option\n");
  2331. avsubtitle_free(sub);
  2332. return AVERROR_INVALIDDATA;
  2333. }
  2334. }
  2335. if (tmp.data != pkt_recoded.data) { // did we recode?
  2336. /* prevent from destroying side data from original packet */
  2337. pkt_recoded.side_data = NULL;
  2338. pkt_recoded.side_data_elems = 0;
  2339. av_free_packet(&pkt_recoded);
  2340. }
  2341. if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
  2342. sub->format = 0;
  2343. else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
  2344. sub->format = 1;
  2345. avctx->internal->pkt = NULL;
  2346. }
  2347. if (did_split) {
  2348. av_packet_free_side_data(&tmp);
  2349. if(ret == tmp.size)
  2350. ret = avpkt->size;
  2351. }
  2352. if (*got_sub_ptr)
  2353. avctx->frame_number++;
  2354. }
  2355. return ret;
  2356. }
  2357. void avsubtitle_free(AVSubtitle *sub)
  2358. {
  2359. int i;
  2360. for (i = 0; i < sub->num_rects; i++) {
  2361. av_freep(&sub->rects[i]->pict.data[0]);
  2362. av_freep(&sub->rects[i]->pict.data[1]);
  2363. av_freep(&sub->rects[i]->pict.data[2]);
  2364. av_freep(&sub->rects[i]->pict.data[3]);
  2365. av_freep(&sub->rects[i]->text);
  2366. av_freep(&sub->rects[i]->ass);
  2367. av_freep(&sub->rects[i]);
  2368. }
  2369. av_freep(&sub->rects);
  2370. memset(sub, 0, sizeof(AVSubtitle));
  2371. }
  2372. av_cold int avcodec_close(AVCodecContext *avctx)
  2373. {
  2374. if (!avctx)
  2375. return 0;
  2376. if (avcodec_is_open(avctx)) {
  2377. FramePool *pool = avctx->internal->pool;
  2378. int i;
  2379. if (CONFIG_FRAME_THREAD_ENCODER &&
  2380. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  2381. ff_frame_thread_encoder_free(avctx);
  2382. }
  2383. if (HAVE_THREADS && avctx->internal->thread_ctx)
  2384. ff_thread_free(avctx);
  2385. if (avctx->codec && avctx->codec->close)
  2386. avctx->codec->close(avctx);
  2387. avctx->coded_frame = NULL;
  2388. avctx->internal->byte_buffer_size = 0;
  2389. av_freep(&avctx->internal->byte_buffer);
  2390. av_frame_free(&avctx->internal->to_free);
  2391. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  2392. av_buffer_pool_uninit(&pool->pools[i]);
  2393. av_freep(&avctx->internal->pool);
  2394. if (avctx->hwaccel && avctx->hwaccel->uninit)
  2395. avctx->hwaccel->uninit(avctx);
  2396. av_freep(&avctx->internal->hwaccel_priv_data);
  2397. av_freep(&avctx->internal);
  2398. }
  2399. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  2400. av_opt_free(avctx->priv_data);
  2401. av_opt_free(avctx);
  2402. av_freep(&avctx->priv_data);
  2403. if (av_codec_is_encoder(avctx->codec))
  2404. av_freep(&avctx->extradata);
  2405. avctx->codec = NULL;
  2406. avctx->active_thread_type = 0;
  2407. return 0;
  2408. }
  2409. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  2410. {
  2411. switch(id){
  2412. //This is for future deprecatec codec ids, its empty since
  2413. //last major bump but will fill up again over time, please don't remove it
  2414. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  2415. case AV_CODEC_ID_BRENDER_PIX_DEPRECATED : return AV_CODEC_ID_BRENDER_PIX;
  2416. case AV_CODEC_ID_OPUS_DEPRECATED : return AV_CODEC_ID_OPUS;
  2417. case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
  2418. case AV_CODEC_ID_PAF_AUDIO_DEPRECATED : return AV_CODEC_ID_PAF_AUDIO;
  2419. case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
  2420. case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
  2421. case AV_CODEC_ID_ADPCM_VIMA_DEPRECATED : return AV_CODEC_ID_ADPCM_VIMA;
  2422. case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
  2423. case AV_CODEC_ID_EXR_DEPRECATED : return AV_CODEC_ID_EXR;
  2424. case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
  2425. case AV_CODEC_ID_PAF_VIDEO_DEPRECATED : return AV_CODEC_ID_PAF_VIDEO;
  2426. case AV_CODEC_ID_WEBP_DEPRECATED : return AV_CODEC_ID_WEBP;
  2427. case AV_CODEC_ID_HEVC_DEPRECATED : return AV_CODEC_ID_HEVC;
  2428. case AV_CODEC_ID_MVC1_DEPRECATED : return AV_CODEC_ID_MVC1;
  2429. case AV_CODEC_ID_MVC2_DEPRECATED : return AV_CODEC_ID_MVC2;
  2430. case AV_CODEC_ID_SANM_DEPRECATED : return AV_CODEC_ID_SANM;
  2431. case AV_CODEC_ID_SGIRLE_DEPRECATED : return AV_CODEC_ID_SGIRLE;
  2432. case AV_CODEC_ID_VP7_DEPRECATED : return AV_CODEC_ID_VP7;
  2433. default : return id;
  2434. }
  2435. }
  2436. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  2437. {
  2438. AVCodec *p, *experimental = NULL;
  2439. p = first_avcodec;
  2440. id= remap_deprecated_codec_id(id);
  2441. while (p) {
  2442. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  2443. p->id == id) {
  2444. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  2445. experimental = p;
  2446. } else
  2447. return p;
  2448. }
  2449. p = p->next;
  2450. }
  2451. return experimental;
  2452. }
  2453. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  2454. {
  2455. return find_encdec(id, 1);
  2456. }
  2457. AVCodec *avcodec_find_encoder_by_name(const char *name)
  2458. {
  2459. AVCodec *p;
  2460. if (!name)
  2461. return NULL;
  2462. p = first_avcodec;
  2463. while (p) {
  2464. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  2465. return p;
  2466. p = p->next;
  2467. }
  2468. return NULL;
  2469. }
  2470. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  2471. {
  2472. return find_encdec(id, 0);
  2473. }
  2474. AVCodec *avcodec_find_decoder_by_name(const char *name)
  2475. {
  2476. AVCodec *p;
  2477. if (!name)
  2478. return NULL;
  2479. p = first_avcodec;
  2480. while (p) {
  2481. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  2482. return p;
  2483. p = p->next;
  2484. }
  2485. return NULL;
  2486. }
  2487. const char *avcodec_get_name(enum AVCodecID id)
  2488. {
  2489. const AVCodecDescriptor *cd;
  2490. AVCodec *codec;
  2491. if (id == AV_CODEC_ID_NONE)
  2492. return "none";
  2493. cd = avcodec_descriptor_get(id);
  2494. if (cd)
  2495. return cd->name;
  2496. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  2497. codec = avcodec_find_decoder(id);
  2498. if (codec)
  2499. return codec->name;
  2500. codec = avcodec_find_encoder(id);
  2501. if (codec)
  2502. return codec->name;
  2503. return "unknown_codec";
  2504. }
  2505. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  2506. {
  2507. int i, len, ret = 0;
  2508. #define TAG_PRINT(x) \
  2509. (((x) >= '0' && (x) <= '9') || \
  2510. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  2511. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  2512. for (i = 0; i < 4; i++) {
  2513. len = snprintf(buf, buf_size,
  2514. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  2515. buf += len;
  2516. buf_size = buf_size > len ? buf_size - len : 0;
  2517. ret += len;
  2518. codec_tag >>= 8;
  2519. }
  2520. return ret;
  2521. }
  2522. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  2523. {
  2524. const char *codec_type;
  2525. const char *codec_name;
  2526. const char *profile = NULL;
  2527. const AVCodec *p;
  2528. int bitrate;
  2529. AVRational display_aspect_ratio;
  2530. if (!buf || buf_size <= 0)
  2531. return;
  2532. codec_type = av_get_media_type_string(enc->codec_type);
  2533. codec_name = avcodec_get_name(enc->codec_id);
  2534. if (enc->profile != FF_PROFILE_UNKNOWN) {
  2535. if (enc->codec)
  2536. p = enc->codec;
  2537. else
  2538. p = encode ? avcodec_find_encoder(enc->codec_id) :
  2539. avcodec_find_decoder(enc->codec_id);
  2540. if (p)
  2541. profile = av_get_profile_name(p, enc->profile);
  2542. }
  2543. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  2544. codec_name);
  2545. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  2546. if (enc->codec && strcmp(enc->codec->name, codec_name))
  2547. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  2548. if (profile)
  2549. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  2550. if (enc->codec_tag) {
  2551. char tag_buf[32];
  2552. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  2553. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2554. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  2555. }
  2556. switch (enc->codec_type) {
  2557. case AVMEDIA_TYPE_VIDEO:
  2558. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  2559. char detail[256] = "(";
  2560. const char *colorspace_name;
  2561. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2562. ", %s",
  2563. av_get_pix_fmt_name(enc->pix_fmt));
  2564. if (enc->bits_per_raw_sample &&
  2565. enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
  2566. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  2567. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  2568. av_strlcatf(detail, sizeof(detail),
  2569. enc->color_range == AVCOL_RANGE_MPEG ? "tv, ": "pc, ");
  2570. colorspace_name = av_get_colorspace_name(enc->colorspace);
  2571. if (colorspace_name)
  2572. av_strlcatf(detail, sizeof(detail), "%s, ", colorspace_name);
  2573. if (strlen(detail) > 1) {
  2574. detail[strlen(detail) - 2] = 0;
  2575. av_strlcatf(buf, buf_size, "%s)", detail);
  2576. }
  2577. }
  2578. if (enc->width) {
  2579. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2580. ", %dx%d",
  2581. enc->width, enc->height);
  2582. if (enc->sample_aspect_ratio.num) {
  2583. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  2584. enc->width * enc->sample_aspect_ratio.num,
  2585. enc->height * enc->sample_aspect_ratio.den,
  2586. 1024 * 1024);
  2587. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2588. " [SAR %d:%d DAR %d:%d]",
  2589. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  2590. display_aspect_ratio.num, display_aspect_ratio.den);
  2591. }
  2592. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2593. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2594. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2595. ", %d/%d",
  2596. enc->time_base.num / g, enc->time_base.den / g);
  2597. }
  2598. }
  2599. if (encode) {
  2600. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2601. ", q=%d-%d", enc->qmin, enc->qmax);
  2602. }
  2603. break;
  2604. case AVMEDIA_TYPE_AUDIO:
  2605. if (enc->sample_rate) {
  2606. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2607. ", %d Hz", enc->sample_rate);
  2608. }
  2609. av_strlcat(buf, ", ", buf_size);
  2610. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  2611. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2612. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2613. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  2614. }
  2615. if ( enc->bits_per_raw_sample > 0
  2616. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  2617. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2618. " (%d bit)", enc->bits_per_raw_sample);
  2619. break;
  2620. case AVMEDIA_TYPE_DATA:
  2621. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2622. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2623. if (g)
  2624. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2625. ", %d/%d",
  2626. enc->time_base.num / g, enc->time_base.den / g);
  2627. }
  2628. break;
  2629. case AVMEDIA_TYPE_SUBTITLE:
  2630. if (enc->width)
  2631. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2632. ", %dx%d", enc->width, enc->height);
  2633. break;
  2634. default:
  2635. return;
  2636. }
  2637. if (encode) {
  2638. if (enc->flags & CODEC_FLAG_PASS1)
  2639. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2640. ", pass 1");
  2641. if (enc->flags & CODEC_FLAG_PASS2)
  2642. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2643. ", pass 2");
  2644. }
  2645. bitrate = get_bit_rate(enc);
  2646. if (bitrate != 0) {
  2647. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2648. ", %d kb/s", bitrate / 1000);
  2649. } else if (enc->rc_max_rate > 0) {
  2650. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2651. ", max. %d kb/s", enc->rc_max_rate / 1000);
  2652. }
  2653. }
  2654. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2655. {
  2656. const AVProfile *p;
  2657. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2658. return NULL;
  2659. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2660. if (p->profile == profile)
  2661. return p->name;
  2662. return NULL;
  2663. }
  2664. unsigned avcodec_version(void)
  2665. {
  2666. // av_assert0(AV_CODEC_ID_V410==164);
  2667. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2668. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2669. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2670. av_assert0(AV_CODEC_ID_SRT==94216);
  2671. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2672. av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
  2673. av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
  2674. av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
  2675. av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
  2676. av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
  2677. return LIBAVCODEC_VERSION_INT;
  2678. }
  2679. const char *avcodec_configuration(void)
  2680. {
  2681. return FFMPEG_CONFIGURATION;
  2682. }
  2683. const char *avcodec_license(void)
  2684. {
  2685. #define LICENSE_PREFIX "libavcodec license: "
  2686. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2687. }
  2688. void avcodec_flush_buffers(AVCodecContext *avctx)
  2689. {
  2690. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2691. ff_thread_flush(avctx);
  2692. else if (avctx->codec->flush)
  2693. avctx->codec->flush(avctx);
  2694. avctx->pts_correction_last_pts =
  2695. avctx->pts_correction_last_dts = INT64_MIN;
  2696. if (!avctx->refcounted_frames)
  2697. av_frame_unref(avctx->internal->to_free);
  2698. }
  2699. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2700. {
  2701. switch (codec_id) {
  2702. case AV_CODEC_ID_8SVX_EXP:
  2703. case AV_CODEC_ID_8SVX_FIB:
  2704. case AV_CODEC_ID_ADPCM_CT:
  2705. case AV_CODEC_ID_ADPCM_IMA_APC:
  2706. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2707. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2708. case AV_CODEC_ID_ADPCM_IMA_WS:
  2709. case AV_CODEC_ID_ADPCM_G722:
  2710. case AV_CODEC_ID_ADPCM_YAMAHA:
  2711. return 4;
  2712. case AV_CODEC_ID_DSD_LSBF:
  2713. case AV_CODEC_ID_DSD_MSBF:
  2714. case AV_CODEC_ID_DSD_LSBF_PLANAR:
  2715. case AV_CODEC_ID_DSD_MSBF_PLANAR:
  2716. case AV_CODEC_ID_PCM_ALAW:
  2717. case AV_CODEC_ID_PCM_MULAW:
  2718. case AV_CODEC_ID_PCM_S8:
  2719. case AV_CODEC_ID_PCM_S8_PLANAR:
  2720. case AV_CODEC_ID_PCM_U8:
  2721. case AV_CODEC_ID_PCM_ZORK:
  2722. return 8;
  2723. case AV_CODEC_ID_PCM_S16BE:
  2724. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2725. case AV_CODEC_ID_PCM_S16LE:
  2726. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2727. case AV_CODEC_ID_PCM_U16BE:
  2728. case AV_CODEC_ID_PCM_U16LE:
  2729. return 16;
  2730. case AV_CODEC_ID_PCM_S24DAUD:
  2731. case AV_CODEC_ID_PCM_S24BE:
  2732. case AV_CODEC_ID_PCM_S24LE:
  2733. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2734. case AV_CODEC_ID_PCM_U24BE:
  2735. case AV_CODEC_ID_PCM_U24LE:
  2736. return 24;
  2737. case AV_CODEC_ID_PCM_S32BE:
  2738. case AV_CODEC_ID_PCM_S32LE:
  2739. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2740. case AV_CODEC_ID_PCM_U32BE:
  2741. case AV_CODEC_ID_PCM_U32LE:
  2742. case AV_CODEC_ID_PCM_F32BE:
  2743. case AV_CODEC_ID_PCM_F32LE:
  2744. return 32;
  2745. case AV_CODEC_ID_PCM_F64BE:
  2746. case AV_CODEC_ID_PCM_F64LE:
  2747. return 64;
  2748. default:
  2749. return 0;
  2750. }
  2751. }
  2752. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2753. {
  2754. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2755. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2756. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2757. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2758. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2759. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2760. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2761. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2762. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2763. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2764. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2765. };
  2766. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2767. return AV_CODEC_ID_NONE;
  2768. if (be < 0 || be > 1)
  2769. be = AV_NE(1, 0);
  2770. return map[fmt][be];
  2771. }
  2772. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2773. {
  2774. switch (codec_id) {
  2775. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2776. return 2;
  2777. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2778. return 3;
  2779. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2780. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2781. case AV_CODEC_ID_ADPCM_IMA_QT:
  2782. case AV_CODEC_ID_ADPCM_SWF:
  2783. case AV_CODEC_ID_ADPCM_MS:
  2784. return 4;
  2785. default:
  2786. return av_get_exact_bits_per_sample(codec_id);
  2787. }
  2788. }
  2789. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2790. {
  2791. int id, sr, ch, ba, tag, bps;
  2792. id = avctx->codec_id;
  2793. sr = avctx->sample_rate;
  2794. ch = avctx->channels;
  2795. ba = avctx->block_align;
  2796. tag = avctx->codec_tag;
  2797. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2798. /* codecs with an exact constant bits per sample */
  2799. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2800. return (frame_bytes * 8LL) / (bps * ch);
  2801. bps = avctx->bits_per_coded_sample;
  2802. /* codecs with a fixed packet duration */
  2803. switch (id) {
  2804. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2805. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2806. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2807. case AV_CODEC_ID_AMR_NB:
  2808. case AV_CODEC_ID_EVRC:
  2809. case AV_CODEC_ID_GSM:
  2810. case AV_CODEC_ID_QCELP:
  2811. case AV_CODEC_ID_RA_288: return 160;
  2812. case AV_CODEC_ID_AMR_WB:
  2813. case AV_CODEC_ID_GSM_MS: return 320;
  2814. case AV_CODEC_ID_MP1: return 384;
  2815. case AV_CODEC_ID_ATRAC1: return 512;
  2816. case AV_CODEC_ID_ATRAC3: return 1024;
  2817. case AV_CODEC_ID_MP2:
  2818. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2819. case AV_CODEC_ID_AC3: return 1536;
  2820. }
  2821. if (sr > 0) {
  2822. /* calc from sample rate */
  2823. if (id == AV_CODEC_ID_TTA)
  2824. return 256 * sr / 245;
  2825. if (ch > 0) {
  2826. /* calc from sample rate and channels */
  2827. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2828. return (480 << (sr / 22050)) / ch;
  2829. }
  2830. }
  2831. if (ba > 0) {
  2832. /* calc from block_align */
  2833. if (id == AV_CODEC_ID_SIPR) {
  2834. switch (ba) {
  2835. case 20: return 160;
  2836. case 19: return 144;
  2837. case 29: return 288;
  2838. case 37: return 480;
  2839. }
  2840. } else if (id == AV_CODEC_ID_ILBC) {
  2841. switch (ba) {
  2842. case 38: return 160;
  2843. case 50: return 240;
  2844. }
  2845. }
  2846. }
  2847. if (frame_bytes > 0) {
  2848. /* calc from frame_bytes only */
  2849. if (id == AV_CODEC_ID_TRUESPEECH)
  2850. return 240 * (frame_bytes / 32);
  2851. if (id == AV_CODEC_ID_NELLYMOSER)
  2852. return 256 * (frame_bytes / 64);
  2853. if (id == AV_CODEC_ID_RA_144)
  2854. return 160 * (frame_bytes / 20);
  2855. if (id == AV_CODEC_ID_G723_1)
  2856. return 240 * (frame_bytes / 24);
  2857. if (bps > 0) {
  2858. /* calc from frame_bytes and bits_per_coded_sample */
  2859. if (id == AV_CODEC_ID_ADPCM_G726)
  2860. return frame_bytes * 8 / bps;
  2861. }
  2862. if (ch > 0) {
  2863. /* calc from frame_bytes and channels */
  2864. switch (id) {
  2865. case AV_CODEC_ID_ADPCM_AFC:
  2866. return frame_bytes / (9 * ch) * 16;
  2867. case AV_CODEC_ID_ADPCM_DTK:
  2868. return frame_bytes / (16 * ch) * 28;
  2869. case AV_CODEC_ID_ADPCM_4XM:
  2870. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2871. return (frame_bytes - 4 * ch) * 2 / ch;
  2872. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2873. return (frame_bytes - 4) * 2 / ch;
  2874. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2875. return (frame_bytes - 8) * 2 / ch;
  2876. case AV_CODEC_ID_ADPCM_XA:
  2877. return (frame_bytes / 128) * 224 / ch;
  2878. case AV_CODEC_ID_INTERPLAY_DPCM:
  2879. return (frame_bytes - 6 - ch) / ch;
  2880. case AV_CODEC_ID_ROQ_DPCM:
  2881. return (frame_bytes - 8) / ch;
  2882. case AV_CODEC_ID_XAN_DPCM:
  2883. return (frame_bytes - 2 * ch) / ch;
  2884. case AV_CODEC_ID_MACE3:
  2885. return 3 * frame_bytes / ch;
  2886. case AV_CODEC_ID_MACE6:
  2887. return 6 * frame_bytes / ch;
  2888. case AV_CODEC_ID_PCM_LXF:
  2889. return 2 * (frame_bytes / (5 * ch));
  2890. case AV_CODEC_ID_IAC:
  2891. case AV_CODEC_ID_IMC:
  2892. return 4 * frame_bytes / ch;
  2893. }
  2894. if (tag) {
  2895. /* calc from frame_bytes, channels, and codec_tag */
  2896. if (id == AV_CODEC_ID_SOL_DPCM) {
  2897. if (tag == 3)
  2898. return frame_bytes / ch;
  2899. else
  2900. return frame_bytes * 2 / ch;
  2901. }
  2902. }
  2903. if (ba > 0) {
  2904. /* calc from frame_bytes, channels, and block_align */
  2905. int blocks = frame_bytes / ba;
  2906. switch (avctx->codec_id) {
  2907. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2908. if (bps < 2 || bps > 5)
  2909. return 0;
  2910. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  2911. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2912. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2913. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2914. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2915. case AV_CODEC_ID_ADPCM_IMA_RAD:
  2916. return blocks * ((ba - 4 * ch) * 2 / ch);
  2917. case AV_CODEC_ID_ADPCM_MS:
  2918. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2919. }
  2920. }
  2921. if (bps > 0) {
  2922. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2923. switch (avctx->codec_id) {
  2924. case AV_CODEC_ID_PCM_DVD:
  2925. if(bps<4)
  2926. return 0;
  2927. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2928. case AV_CODEC_ID_PCM_BLURAY:
  2929. if(bps<4)
  2930. return 0;
  2931. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2932. case AV_CODEC_ID_S302M:
  2933. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2934. }
  2935. }
  2936. }
  2937. }
  2938. /* Fall back on using frame_size */
  2939. if (avctx->frame_size > 1 && frame_bytes)
  2940. return avctx->frame_size;
  2941. //For WMA we currently have no other means to calculate duration thus we
  2942. //do it here by assuming CBR, which is true for all known cases.
  2943. if (avctx->bit_rate>0 && frame_bytes>0 && avctx->sample_rate>0 && avctx->block_align>1) {
  2944. if (avctx->codec_id == AV_CODEC_ID_WMAV1 || avctx->codec_id == AV_CODEC_ID_WMAV2)
  2945. return (frame_bytes * 8LL * avctx->sample_rate) / avctx->bit_rate;
  2946. }
  2947. return 0;
  2948. }
  2949. #if !HAVE_THREADS
  2950. int ff_thread_init(AVCodecContext *s)
  2951. {
  2952. return -1;
  2953. }
  2954. #endif
  2955. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2956. {
  2957. unsigned int n = 0;
  2958. while (v >= 0xff) {
  2959. *s++ = 0xff;
  2960. v -= 0xff;
  2961. n++;
  2962. }
  2963. *s = v;
  2964. n++;
  2965. return n;
  2966. }
  2967. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2968. {
  2969. int i;
  2970. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2971. return i;
  2972. }
  2973. #if FF_API_MISSING_SAMPLE
  2974. FF_DISABLE_DEPRECATION_WARNINGS
  2975. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2976. {
  2977. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2978. "version to the newest one from Git. If the problem still "
  2979. "occurs, it means that your file has a feature which has not "
  2980. "been implemented.\n", feature);
  2981. if(want_sample)
  2982. av_log_ask_for_sample(avc, NULL);
  2983. }
  2984. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2985. {
  2986. va_list argument_list;
  2987. va_start(argument_list, msg);
  2988. if (msg)
  2989. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2990. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2991. "of this file to ftp://upload.ffmpeg.org/incoming/ "
  2992. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
  2993. va_end(argument_list);
  2994. }
  2995. FF_ENABLE_DEPRECATION_WARNINGS
  2996. #endif /* FF_API_MISSING_SAMPLE */
  2997. static AVHWAccel *first_hwaccel = NULL;
  2998. static AVHWAccel **last_hwaccel = &first_hwaccel;
  2999. void av_register_hwaccel(AVHWAccel *hwaccel)
  3000. {
  3001. AVHWAccel **p = last_hwaccel;
  3002. hwaccel->next = NULL;
  3003. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
  3004. p = &(*p)->next;
  3005. last_hwaccel = &hwaccel->next;
  3006. }
  3007. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  3008. {
  3009. return hwaccel ? hwaccel->next : first_hwaccel;
  3010. }
  3011. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  3012. {
  3013. if (lockmgr_cb) {
  3014. if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  3015. return -1;
  3016. if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  3017. return -1;
  3018. }
  3019. lockmgr_cb = cb;
  3020. if (lockmgr_cb) {
  3021. if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  3022. return -1;
  3023. if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  3024. return -1;
  3025. }
  3026. return 0;
  3027. }
  3028. int ff_lock_avcodec(AVCodecContext *log_ctx)
  3029. {
  3030. if (lockmgr_cb) {
  3031. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  3032. return -1;
  3033. }
  3034. entangled_thread_counter++;
  3035. if (entangled_thread_counter != 1) {
  3036. av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  3037. if (!lockmgr_cb)
  3038. av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
  3039. ff_avcodec_locked = 1;
  3040. ff_unlock_avcodec();
  3041. return AVERROR(EINVAL);
  3042. }
  3043. av_assert0(!ff_avcodec_locked);
  3044. ff_avcodec_locked = 1;
  3045. return 0;
  3046. }
  3047. int ff_unlock_avcodec(void)
  3048. {
  3049. av_assert0(ff_avcodec_locked);
  3050. ff_avcodec_locked = 0;
  3051. entangled_thread_counter--;
  3052. if (lockmgr_cb) {
  3053. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  3054. return -1;
  3055. }
  3056. return 0;
  3057. }
  3058. int avpriv_lock_avformat(void)
  3059. {
  3060. if (lockmgr_cb) {
  3061. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  3062. return -1;
  3063. }
  3064. return 0;
  3065. }
  3066. int avpriv_unlock_avformat(void)
  3067. {
  3068. if (lockmgr_cb) {
  3069. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  3070. return -1;
  3071. }
  3072. return 0;
  3073. }
  3074. unsigned int avpriv_toupper4(unsigned int x)
  3075. {
  3076. return av_toupper(x & 0xFF) +
  3077. (av_toupper((x >> 8) & 0xFF) << 8) +
  3078. (av_toupper((x >> 16) & 0xFF) << 16) +
  3079. ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
  3080. }
  3081. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  3082. {
  3083. int ret;
  3084. dst->owner = src->owner;
  3085. ret = av_frame_ref(dst->f, src->f);
  3086. if (ret < 0)
  3087. return ret;
  3088. if (src->progress &&
  3089. !(dst->progress = av_buffer_ref(src->progress))) {
  3090. ff_thread_release_buffer(dst->owner, dst);
  3091. return AVERROR(ENOMEM);
  3092. }
  3093. return 0;
  3094. }
  3095. #if !HAVE_THREADS
  3096. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  3097. {
  3098. return ff_get_format(avctx, fmt);
  3099. }
  3100. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  3101. {
  3102. f->owner = avctx;
  3103. return ff_get_buffer(avctx, f->f, flags);
  3104. }
  3105. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  3106. {
  3107. if (f->f)
  3108. av_frame_unref(f->f);
  3109. }
  3110. void ff_thread_finish_setup(AVCodecContext *avctx)
  3111. {
  3112. }
  3113. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  3114. {
  3115. }
  3116. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  3117. {
  3118. }
  3119. int ff_thread_can_start_frame(AVCodecContext *avctx)
  3120. {
  3121. return 1;
  3122. }
  3123. int ff_alloc_entries(AVCodecContext *avctx, int count)
  3124. {
  3125. return 0;
  3126. }
  3127. void ff_reset_entries(AVCodecContext *avctx)
  3128. {
  3129. }
  3130. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  3131. {
  3132. }
  3133. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  3134. {
  3135. }
  3136. #endif
  3137. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  3138. {
  3139. AVCodec *c= avcodec_find_decoder(codec_id);
  3140. if(!c)
  3141. c= avcodec_find_encoder(codec_id);
  3142. if(c)
  3143. return c->type;
  3144. if (codec_id <= AV_CODEC_ID_NONE)
  3145. return AVMEDIA_TYPE_UNKNOWN;
  3146. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  3147. return AVMEDIA_TYPE_VIDEO;
  3148. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  3149. return AVMEDIA_TYPE_AUDIO;
  3150. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  3151. return AVMEDIA_TYPE_SUBTITLE;
  3152. return AVMEDIA_TYPE_UNKNOWN;
  3153. }
  3154. int avcodec_is_open(AVCodecContext *s)
  3155. {
  3156. return !!s->internal;
  3157. }
  3158. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  3159. {
  3160. int ret;
  3161. char *str;
  3162. ret = av_bprint_finalize(buf, &str);
  3163. if (ret < 0)
  3164. return ret;
  3165. if (!av_bprint_is_complete(buf)) {
  3166. av_free(str);
  3167. return AVERROR(ENOMEM);
  3168. }
  3169. avctx->extradata = str;
  3170. /* Note: the string is NUL terminated (so extradata can be read as a
  3171. * string), but the ending character is not accounted in the size (in
  3172. * binary formats you are likely not supposed to mux that character). When
  3173. * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
  3174. * zeros. */
  3175. avctx->extradata_size = buf->len;
  3176. return 0;
  3177. }
  3178. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  3179. const uint8_t *end,
  3180. uint32_t *av_restrict state)
  3181. {
  3182. int i;
  3183. av_assert0(p <= end);
  3184. if (p >= end)
  3185. return end;
  3186. for (i = 0; i < 3; i++) {
  3187. uint32_t tmp = *state << 8;
  3188. *state = tmp + *(p++);
  3189. if (tmp == 0x100 || p == end)
  3190. return p;
  3191. }
  3192. while (p < end) {
  3193. if (p[-1] > 1 ) p += 3;
  3194. else if (p[-2] ) p += 2;
  3195. else if (p[-3]|(p[-1]-1)) p++;
  3196. else {
  3197. p++;
  3198. break;
  3199. }
  3200. }
  3201. p = FFMIN(p, end) - 4;
  3202. *state = AV_RB32(p);
  3203. return p + 4;
  3204. }