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.

3671 lines
120KB

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