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.

3598 lines
117KB

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