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.

3549 lines
115KB

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