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.

3850 lines
127KB

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