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.

3656 lines
119KB

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