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.

3655 lines
120KB

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