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.

2591 lines
83KB

  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 "libavutil/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "libavutil/dict.h"
  35. #include "libavutil/avassert.h"
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. #include "libavutil/opt.h"
  39. #include "thread.h"
  40. #include "frame_thread_encoder.h"
  41. #include "audioconvert.h"
  42. #include "internal.h"
  43. #include "bytestream.h"
  44. #include <stdlib.h>
  45. #include <stdarg.h>
  46. #include <limits.h>
  47. #include <float.h>
  48. static int volatile entangled_thread_counter = 0;
  49. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  50. static void *codec_mutex;
  51. static void *avformat_mutex;
  52. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  53. {
  54. if (min_size < *size)
  55. return ptr;
  56. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  57. ptr = av_realloc(ptr, min_size);
  58. /* we could set this to the unmodified min_size but this is safer
  59. * if the user lost the ptr and uses NULL now
  60. */
  61. if (!ptr)
  62. min_size = 0;
  63. *size = min_size;
  64. return ptr;
  65. }
  66. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  67. {
  68. void **p = ptr;
  69. if (min_size < *size)
  70. return 0;
  71. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  72. av_free(*p);
  73. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  74. if (!*p)
  75. min_size = 0;
  76. *size = min_size;
  77. return 1;
  78. }
  79. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  80. {
  81. ff_fast_malloc(ptr, size, min_size, 0);
  82. }
  83. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  84. {
  85. uint8_t **p = ptr;
  86. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  87. av_freep(p);
  88. *size = 0;
  89. return;
  90. }
  91. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  92. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  93. }
  94. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  95. {
  96. uint8_t **p = ptr;
  97. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  98. av_freep(p);
  99. *size = 0;
  100. return;
  101. }
  102. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  103. memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  104. }
  105. /* encoder management */
  106. static AVCodec *first_avcodec = NULL;
  107. AVCodec *av_codec_next(const AVCodec *c)
  108. {
  109. if (c)
  110. return c->next;
  111. else
  112. return first_avcodec;
  113. }
  114. static void avcodec_init(void)
  115. {
  116. static int initialized = 0;
  117. if (initialized != 0)
  118. return;
  119. initialized = 1;
  120. ff_dsputil_static_init();
  121. }
  122. int av_codec_is_encoder(const AVCodec *codec)
  123. {
  124. return codec && (codec->encode_sub || codec->encode2);
  125. }
  126. int av_codec_is_decoder(const AVCodec *codec)
  127. {
  128. return codec && codec->decode;
  129. }
  130. void avcodec_register(AVCodec *codec)
  131. {
  132. AVCodec **p;
  133. avcodec_init();
  134. p = &first_avcodec;
  135. while (*p != NULL)
  136. p = &(*p)->next;
  137. *p = codec;
  138. codec->next = NULL;
  139. if (codec->init_static_data)
  140. codec->init_static_data(codec);
  141. }
  142. unsigned avcodec_get_edge_width(void)
  143. {
  144. return EDGE_WIDTH;
  145. }
  146. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  147. {
  148. s->coded_width = width;
  149. s->coded_height = height;
  150. s->width = -((-width ) >> s->lowres);
  151. s->height = -((-height) >> s->lowres);
  152. }
  153. #define INTERNAL_BUFFER_SIZE (32 + 1)
  154. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  155. int linesize_align[AV_NUM_DATA_POINTERS])
  156. {
  157. int i;
  158. int w_align = 1;
  159. int h_align = 1;
  160. switch (s->pix_fmt) {
  161. case AV_PIX_FMT_YUV420P:
  162. case AV_PIX_FMT_YUYV422:
  163. case AV_PIX_FMT_UYVY422:
  164. case AV_PIX_FMT_YUV422P:
  165. case AV_PIX_FMT_YUV440P:
  166. case AV_PIX_FMT_YUV444P:
  167. case AV_PIX_FMT_GBRP:
  168. case AV_PIX_FMT_GRAY8:
  169. case AV_PIX_FMT_GRAY16BE:
  170. case AV_PIX_FMT_GRAY16LE:
  171. case AV_PIX_FMT_YUVJ420P:
  172. case AV_PIX_FMT_YUVJ422P:
  173. case AV_PIX_FMT_YUVJ440P:
  174. case AV_PIX_FMT_YUVJ444P:
  175. case AV_PIX_FMT_YUVA420P:
  176. case AV_PIX_FMT_YUVA422P:
  177. case AV_PIX_FMT_YUVA444P:
  178. case AV_PIX_FMT_YUV420P9LE:
  179. case AV_PIX_FMT_YUV420P9BE:
  180. case AV_PIX_FMT_YUV420P10LE:
  181. case AV_PIX_FMT_YUV420P10BE:
  182. case AV_PIX_FMT_YUV420P12LE:
  183. case AV_PIX_FMT_YUV420P12BE:
  184. case AV_PIX_FMT_YUV420P14LE:
  185. case AV_PIX_FMT_YUV420P14BE:
  186. case AV_PIX_FMT_YUV422P9LE:
  187. case AV_PIX_FMT_YUV422P9BE:
  188. case AV_PIX_FMT_YUV422P10LE:
  189. case AV_PIX_FMT_YUV422P10BE:
  190. case AV_PIX_FMT_YUV422P12LE:
  191. case AV_PIX_FMT_YUV422P12BE:
  192. case AV_PIX_FMT_YUV422P14LE:
  193. case AV_PIX_FMT_YUV422P14BE:
  194. case AV_PIX_FMT_YUV444P9LE:
  195. case AV_PIX_FMT_YUV444P9BE:
  196. case AV_PIX_FMT_YUV444P10LE:
  197. case AV_PIX_FMT_YUV444P10BE:
  198. case AV_PIX_FMT_YUV444P12LE:
  199. case AV_PIX_FMT_YUV444P12BE:
  200. case AV_PIX_FMT_YUV444P14LE:
  201. case AV_PIX_FMT_YUV444P14BE:
  202. case AV_PIX_FMT_GBRP9LE:
  203. case AV_PIX_FMT_GBRP9BE:
  204. case AV_PIX_FMT_GBRP10LE:
  205. case AV_PIX_FMT_GBRP10BE:
  206. case AV_PIX_FMT_GBRP12LE:
  207. case AV_PIX_FMT_GBRP12BE:
  208. case AV_PIX_FMT_GBRP14LE:
  209. case AV_PIX_FMT_GBRP14BE:
  210. w_align = 16; //FIXME assume 16 pixel per macroblock
  211. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  212. break;
  213. case AV_PIX_FMT_YUV411P:
  214. case AV_PIX_FMT_UYYVYY411:
  215. w_align = 32;
  216. h_align = 8;
  217. break;
  218. case AV_PIX_FMT_YUV410P:
  219. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  220. w_align = 64;
  221. h_align = 64;
  222. }
  223. case AV_PIX_FMT_RGB555:
  224. if (s->codec_id == AV_CODEC_ID_RPZA) {
  225. w_align = 4;
  226. h_align = 4;
  227. }
  228. case AV_PIX_FMT_PAL8:
  229. case AV_PIX_FMT_BGR8:
  230. case AV_PIX_FMT_RGB8:
  231. if (s->codec_id == AV_CODEC_ID_SMC) {
  232. w_align = 4;
  233. h_align = 4;
  234. }
  235. break;
  236. case AV_PIX_FMT_BGR24:
  237. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  238. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  239. w_align = 4;
  240. h_align = 4;
  241. }
  242. break;
  243. default:
  244. w_align = 1;
  245. h_align = 1;
  246. break;
  247. }
  248. if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
  249. w_align = FFMAX(w_align, 8);
  250. }
  251. *width = FFALIGN(*width, w_align);
  252. *height = FFALIGN(*height, h_align);
  253. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
  254. // some of the optimized chroma MC reads one line too much
  255. // which is also done in mpeg decoders with lowres > 0
  256. *height += 2;
  257. for (i = 0; i < 4; i++)
  258. linesize_align[i] = STRIDE_ALIGN;
  259. }
  260. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  261. {
  262. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  263. int linesize_align[AV_NUM_DATA_POINTERS];
  264. int align;
  265. avcodec_align_dimensions2(s, width, height, linesize_align);
  266. align = FFMAX(linesize_align[0], linesize_align[3]);
  267. linesize_align[1] <<= chroma_shift;
  268. linesize_align[2] <<= chroma_shift;
  269. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  270. *width = FFALIGN(*width, align);
  271. }
  272. void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
  273. {
  274. if (s->pkt) {
  275. frame->pkt_pts = s->pkt->pts;
  276. frame->pkt_pos = s->pkt->pos;
  277. frame->pkt_duration = s->pkt->duration;
  278. } else {
  279. frame->pkt_pts = AV_NOPTS_VALUE;
  280. frame->pkt_pos = -1;
  281. frame->pkt_duration = 0;
  282. }
  283. frame->reordered_opaque = s->reordered_opaque;
  284. switch (s->codec->type) {
  285. case AVMEDIA_TYPE_VIDEO:
  286. frame->width = s->width;
  287. frame->height = s->height;
  288. frame->format = s->pix_fmt;
  289. frame->sample_aspect_ratio = s->sample_aspect_ratio;
  290. break;
  291. case AVMEDIA_TYPE_AUDIO:
  292. frame->sample_rate = s->sample_rate;
  293. frame->format = s->sample_fmt;
  294. frame->channel_layout = s->channel_layout;
  295. frame->channels = s->channels;
  296. break;
  297. }
  298. }
  299. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  300. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  301. int buf_size, int align)
  302. {
  303. int ch, planar, needed_size, ret = 0;
  304. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  305. frame->nb_samples, sample_fmt,
  306. align);
  307. if (buf_size < needed_size)
  308. return AVERROR(EINVAL);
  309. planar = av_sample_fmt_is_planar(sample_fmt);
  310. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  311. if (!(frame->extended_data = av_mallocz(nb_channels *
  312. sizeof(*frame->extended_data))))
  313. return AVERROR(ENOMEM);
  314. } else {
  315. frame->extended_data = frame->data;
  316. }
  317. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  318. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  319. sample_fmt, align)) < 0) {
  320. if (frame->extended_data != frame->data)
  321. av_freep(&frame->extended_data);
  322. return ret;
  323. }
  324. if (frame->extended_data != frame->data) {
  325. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  326. frame->data[ch] = frame->extended_data[ch];
  327. }
  328. return ret;
  329. }
  330. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  331. {
  332. AVCodecInternal *avci = avctx->internal;
  333. InternalBuffer *buf;
  334. int buf_size, ret;
  335. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  336. frame->nb_samples, avctx->sample_fmt,
  337. 0);
  338. if (buf_size < 0)
  339. return AVERROR(EINVAL);
  340. /* allocate InternalBuffer if needed */
  341. if (!avci->buffer) {
  342. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  343. if (!avci->buffer)
  344. return AVERROR(ENOMEM);
  345. }
  346. buf = avci->buffer;
  347. /* if there is a previously-used internal buffer, check its size and
  348. * channel count to see if we can reuse it */
  349. if (buf->extended_data) {
  350. /* if current buffer is too small, free it */
  351. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  352. av_free(buf->extended_data[0]);
  353. if (buf->extended_data != buf->data)
  354. av_free(buf->extended_data);
  355. buf->extended_data = NULL;
  356. buf->data[0] = NULL;
  357. }
  358. /* if number of channels has changed, reset and/or free extended data
  359. * pointers but leave data buffer in buf->data[0] for reuse */
  360. if (buf->nb_channels != avctx->channels) {
  361. if (buf->extended_data != buf->data)
  362. av_free(buf->extended_data);
  363. buf->extended_data = NULL;
  364. }
  365. }
  366. /* if there is no previous buffer or the previous buffer cannot be used
  367. * as-is, allocate a new buffer and/or rearrange the channel pointers */
  368. if (!buf->extended_data) {
  369. if (!buf->data[0]) {
  370. if (!(buf->data[0] = av_mallocz(buf_size)))
  371. return AVERROR(ENOMEM);
  372. buf->audio_data_size = buf_size;
  373. }
  374. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  375. avctx->sample_fmt, buf->data[0],
  376. buf->audio_data_size, 0)))
  377. return ret;
  378. if (frame->extended_data == frame->data)
  379. buf->extended_data = buf->data;
  380. else
  381. buf->extended_data = frame->extended_data;
  382. memcpy(buf->data, frame->data, sizeof(frame->data));
  383. buf->linesize[0] = frame->linesize[0];
  384. buf->nb_channels = avctx->channels;
  385. } else {
  386. /* copy InternalBuffer info to the AVFrame */
  387. frame->extended_data = buf->extended_data;
  388. frame->linesize[0] = buf->linesize[0];
  389. memcpy(frame->data, buf->data, sizeof(frame->data));
  390. }
  391. frame->type = FF_BUFFER_TYPE_INTERNAL;
  392. ff_init_buffer_info(avctx, frame);
  393. if (avctx->debug & FF_DEBUG_BUFFERS)
  394. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  395. "internal audio buffer used\n", frame);
  396. return 0;
  397. }
  398. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  399. {
  400. int i;
  401. int w = s->width;
  402. int h = s->height;
  403. InternalBuffer *buf;
  404. AVCodecInternal *avci = s->internal;
  405. if (pic->data[0] != NULL) {
  406. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  407. return -1;
  408. }
  409. if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  410. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  411. return -1;
  412. }
  413. if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
  414. av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  415. return -1;
  416. }
  417. if (!avci->buffer) {
  418. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
  419. sizeof(InternalBuffer));
  420. }
  421. buf = &avci->buffer[avci->buffer_count];
  422. if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
  423. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  424. av_freep(&buf->base[i]);
  425. buf->data[i] = NULL;
  426. }
  427. }
  428. if (!buf->base[0]) {
  429. int h_chroma_shift, v_chroma_shift;
  430. int size[4] = { 0 };
  431. int tmpsize;
  432. int unaligned;
  433. AVPicture picture;
  434. int stride_align[AV_NUM_DATA_POINTERS];
  435. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1 + 1;
  436. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  437. avcodec_align_dimensions2(s, &w, &h, stride_align);
  438. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  439. w += EDGE_WIDTH * 2;
  440. h += EDGE_WIDTH * 2;
  441. }
  442. do {
  443. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  444. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  445. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  446. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  447. w += w & ~(w - 1);
  448. unaligned = 0;
  449. for (i = 0; i < 4; i++)
  450. unaligned |= picture.linesize[i] % stride_align[i];
  451. } while (unaligned);
  452. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  453. if (tmpsize < 0)
  454. return -1;
  455. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  456. size[i] = picture.data[i + 1] - picture.data[i];
  457. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  458. memset(buf->base, 0, sizeof(buf->base));
  459. memset(buf->data, 0, sizeof(buf->data));
  460. for (i = 0; i < 4 && size[i]; i++) {
  461. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  462. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  463. buf->linesize[i] = picture.linesize[i];
  464. buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
  465. if (buf->base[i] == NULL)
  466. return AVERROR(ENOMEM);
  467. memset(buf->base[i], 128, size[i]);
  468. // no edge if EDGE EMU or not planar YUV
  469. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
  470. buf->data[i] = buf->base[i];
  471. else
  472. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
  473. }
  474. for (; i < AV_NUM_DATA_POINTERS; i++) {
  475. buf->base[i] = buf->data[i] = NULL;
  476. buf->linesize[i] = 0;
  477. }
  478. if (size[1] && !size[2])
  479. ff_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
  480. buf->width = s->width;
  481. buf->height = s->height;
  482. buf->pix_fmt = s->pix_fmt;
  483. }
  484. pic->type = FF_BUFFER_TYPE_INTERNAL;
  485. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  486. pic->base[i] = buf->base[i];
  487. pic->data[i] = buf->data[i];
  488. pic->linesize[i] = buf->linesize[i];
  489. }
  490. pic->extended_data = pic->data;
  491. avci->buffer_count++;
  492. pic->width = buf->width;
  493. pic->height = buf->height;
  494. pic->format = buf->pix_fmt;
  495. ff_init_buffer_info(s, pic);
  496. if (s->debug & FF_DEBUG_BUFFERS)
  497. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  498. "buffers used\n", pic, avci->buffer_count);
  499. return 0;
  500. }
  501. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  502. {
  503. switch (avctx->codec_type) {
  504. case AVMEDIA_TYPE_VIDEO:
  505. return video_get_buffer(avctx, frame);
  506. case AVMEDIA_TYPE_AUDIO:
  507. return audio_get_buffer(avctx, frame);
  508. default:
  509. return -1;
  510. }
  511. }
  512. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  513. {
  514. int i;
  515. InternalBuffer *buf, *last;
  516. AVCodecInternal *avci = s->internal;
  517. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  518. assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
  519. assert(avci->buffer_count);
  520. if (avci->buffer) {
  521. buf = NULL; /* avoids warning */
  522. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  523. buf = &avci->buffer[i];
  524. if (buf->data[0] == pic->data[0])
  525. break;
  526. }
  527. av_assert0(i < avci->buffer_count);
  528. avci->buffer_count--;
  529. last = &avci->buffer[avci->buffer_count];
  530. if (buf != last)
  531. FFSWAP(InternalBuffer, *buf, *last);
  532. }
  533. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  534. pic->data[i] = NULL;
  535. // pic->base[i]=NULL;
  536. if (s->debug & FF_DEBUG_BUFFERS)
  537. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  538. "buffers used\n", pic, avci->buffer_count);
  539. }
  540. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  541. {
  542. AVFrame temp_pic;
  543. int i;
  544. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  545. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  546. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  547. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  548. s->release_buffer(s, pic);
  549. }
  550. ff_init_buffer_info(s, pic);
  551. /* If no picture return a new buffer */
  552. if (pic->data[0] == NULL) {
  553. /* We will copy from buffer, so must be readable */
  554. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  555. return s->get_buffer(s, pic);
  556. }
  557. assert(s->pix_fmt == pic->format);
  558. /* If internal buffer type return the same buffer */
  559. if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
  560. return 0;
  561. }
  562. /*
  563. * Not internal type and reget_buffer not overridden, emulate cr buffer
  564. */
  565. temp_pic = *pic;
  566. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  567. pic->data[i] = pic->base[i] = NULL;
  568. pic->opaque = NULL;
  569. /* Allocate new frame */
  570. if (s->get_buffer(s, pic))
  571. return -1;
  572. /* Copy image data from old buffer to new buffer */
  573. av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
  574. s->height);
  575. s->release_buffer(s, &temp_pic); // Release old frame
  576. return 0;
  577. }
  578. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  579. {
  580. int i;
  581. for (i = 0; i < count; i++) {
  582. int r = func(c, (char *)arg + i * size);
  583. if (ret)
  584. ret[i] = r;
  585. }
  586. return 0;
  587. }
  588. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  589. {
  590. int i;
  591. for (i = 0; i < count; i++) {
  592. int r = func(c, arg, i, 0);
  593. if (ret)
  594. ret[i] = r;
  595. }
  596. return 0;
  597. }
  598. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  599. {
  600. while (*fmt != AV_PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  601. ++fmt;
  602. return fmt[0];
  603. }
  604. void avcodec_get_frame_defaults(AVFrame *frame)
  605. {
  606. #if LIBAVCODEC_VERSION_MAJOR >= 55
  607. // extended_data should explicitly be freed when needed, this code is unsafe currently
  608. // also this is not compatible to the <55 ABI/API
  609. if (frame->extended_data != frame->data && 0)
  610. av_freep(&frame->extended_data);
  611. #endif
  612. memset(frame, 0, sizeof(AVFrame));
  613. frame->pts =
  614. frame->pkt_dts =
  615. frame->pkt_pts =
  616. frame->best_effort_timestamp = AV_NOPTS_VALUE;
  617. frame->pkt_duration = 0;
  618. frame->pkt_pos = -1;
  619. frame->key_frame = 1;
  620. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  621. frame->format = -1; /* unknown */
  622. frame->extended_data = frame->data;
  623. }
  624. AVFrame *avcodec_alloc_frame(void)
  625. {
  626. AVFrame *frame = av_malloc(sizeof(AVFrame));
  627. if (frame == NULL)
  628. return NULL;
  629. frame->extended_data = NULL;
  630. avcodec_get_frame_defaults(frame);
  631. return frame;
  632. }
  633. void avcodec_free_frame(AVFrame **frame)
  634. {
  635. AVFrame *f;
  636. if (!frame || !*frame)
  637. return;
  638. f = *frame;
  639. if (f->extended_data != f->data)
  640. av_freep(&f->extended_data);
  641. av_freep(frame);
  642. }
  643. #define MAKE_ACCESSORS(str, name, type, field) \
  644. type av_##name##_get_##field(const str *s) { return s->field; } \
  645. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  646. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  647. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  648. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  649. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  650. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  651. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  652. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  653. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  654. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  655. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  656. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  657. {
  658. memset(sub, 0, sizeof(*sub));
  659. sub->pts = AV_NOPTS_VALUE;
  660. }
  661. static int get_bit_rate(AVCodecContext *ctx)
  662. {
  663. int bit_rate;
  664. int bits_per_sample;
  665. switch (ctx->codec_type) {
  666. case AVMEDIA_TYPE_VIDEO:
  667. case AVMEDIA_TYPE_DATA:
  668. case AVMEDIA_TYPE_SUBTITLE:
  669. case AVMEDIA_TYPE_ATTACHMENT:
  670. bit_rate = ctx->bit_rate;
  671. break;
  672. case AVMEDIA_TYPE_AUDIO:
  673. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  674. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  675. break;
  676. default:
  677. bit_rate = 0;
  678. break;
  679. }
  680. return bit_rate;
  681. }
  682. #if FF_API_AVCODEC_OPEN
  683. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  684. {
  685. return avcodec_open2(avctx, codec, NULL);
  686. }
  687. #endif
  688. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  689. {
  690. int ret = 0;
  691. AVDictionary *tmp = NULL;
  692. if (avcodec_is_open(avctx))
  693. return 0;
  694. if ((!codec && !avctx->codec)) {
  695. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  696. return AVERROR(EINVAL);
  697. }
  698. if ((codec && avctx->codec && codec != avctx->codec)) {
  699. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  700. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  701. return AVERROR(EINVAL);
  702. }
  703. if (!codec)
  704. codec = avctx->codec;
  705. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  706. return AVERROR(EINVAL);
  707. if (options)
  708. av_dict_copy(&tmp, *options, 0);
  709. /* If there is a user-supplied mutex locking routine, call it. */
  710. if (ff_lockmgr_cb) {
  711. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  712. return -1;
  713. }
  714. entangled_thread_counter++;
  715. if (entangled_thread_counter != 1) {
  716. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  717. ret = -1;
  718. goto end;
  719. }
  720. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  721. if (!avctx->internal) {
  722. ret = AVERROR(ENOMEM);
  723. goto end;
  724. }
  725. if (codec->priv_data_size > 0) {
  726. if (!avctx->priv_data) {
  727. avctx->priv_data = av_mallocz(codec->priv_data_size);
  728. if (!avctx->priv_data) {
  729. ret = AVERROR(ENOMEM);
  730. goto end;
  731. }
  732. if (codec->priv_class) {
  733. *(const AVClass **)avctx->priv_data = codec->priv_class;
  734. av_opt_set_defaults(avctx->priv_data);
  735. }
  736. }
  737. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  738. goto free_and_end;
  739. } else {
  740. avctx->priv_data = NULL;
  741. }
  742. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  743. goto free_and_end;
  744. if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
  745. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  746. av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, try -strict -2\n");
  747. ret = -1;
  748. goto free_and_end;
  749. }
  750. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  751. if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
  752. if (avctx->coded_width && avctx->coded_height)
  753. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  754. else if (avctx->width && avctx->height)
  755. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  756. }
  757. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  758. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  759. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  760. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  761. avcodec_set_dimensions(avctx, 0, 0);
  762. }
  763. /* if the decoder init function was already called previously,
  764. * free the already allocated subtitle_header before overwriting it */
  765. if (av_codec_is_decoder(codec))
  766. av_freep(&avctx->subtitle_header);
  767. #define SANE_NB_CHANNELS 128U
  768. if (avctx->channels > SANE_NB_CHANNELS) {
  769. ret = AVERROR(EINVAL);
  770. goto free_and_end;
  771. }
  772. avctx->codec = codec;
  773. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  774. avctx->codec_id == AV_CODEC_ID_NONE) {
  775. avctx->codec_type = codec->type;
  776. avctx->codec_id = codec->id;
  777. }
  778. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  779. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  780. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  781. ret = AVERROR(EINVAL);
  782. goto free_and_end;
  783. }
  784. avctx->frame_number = 0;
  785. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  786. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  787. (!avctx->time_base.num || !avctx->time_base.den)) {
  788. avctx->time_base.num = 1;
  789. avctx->time_base.den = avctx->sample_rate;
  790. }
  791. if (!HAVE_THREADS)
  792. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  793. if (HAVE_THREADS) {
  794. entangled_thread_counter--; //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  795. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  796. entangled_thread_counter++;
  797. if (ret < 0)
  798. goto free_and_end;
  799. }
  800. if (HAVE_THREADS && !avctx->thread_opaque
  801. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  802. ret = ff_thread_init(avctx);
  803. if (ret < 0) {
  804. goto free_and_end;
  805. }
  806. }
  807. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  808. avctx->thread_count = 1;
  809. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  810. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  811. avctx->codec->max_lowres);
  812. ret = AVERROR(EINVAL);
  813. goto free_and_end;
  814. }
  815. if (av_codec_is_encoder(avctx->codec)) {
  816. int i;
  817. if (avctx->codec->sample_fmts) {
  818. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  819. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  820. break;
  821. if (avctx->channels == 1 &&
  822. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  823. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  824. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  825. break;
  826. }
  827. }
  828. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  829. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  830. ret = AVERROR(EINVAL);
  831. goto free_and_end;
  832. }
  833. }
  834. if (avctx->codec->pix_fmts) {
  835. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  836. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  837. break;
  838. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  839. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  840. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  841. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  842. ret = AVERROR(EINVAL);
  843. goto free_and_end;
  844. }
  845. }
  846. if (avctx->codec->supported_samplerates) {
  847. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  848. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  849. break;
  850. if (avctx->codec->supported_samplerates[i] == 0) {
  851. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  852. ret = AVERROR(EINVAL);
  853. goto free_and_end;
  854. }
  855. }
  856. if (avctx->codec->channel_layouts) {
  857. if (!avctx->channel_layout) {
  858. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  859. } else {
  860. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  861. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  862. break;
  863. if (avctx->codec->channel_layouts[i] == 0) {
  864. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  865. ret = AVERROR(EINVAL);
  866. goto free_and_end;
  867. }
  868. }
  869. }
  870. if (avctx->channel_layout && avctx->channels) {
  871. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  872. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  873. ret = AVERROR(EINVAL);
  874. goto free_and_end;
  875. }
  876. } else if (avctx->channel_layout) {
  877. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  878. }
  879. }
  880. avctx->pts_correction_num_faulty_pts =
  881. avctx->pts_correction_num_faulty_dts = 0;
  882. avctx->pts_correction_last_pts =
  883. avctx->pts_correction_last_dts = INT64_MIN;
  884. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  885. || avctx->internal->frame_thread_encoder)) {
  886. ret = avctx->codec->init(avctx);
  887. if (ret < 0) {
  888. goto free_and_end;
  889. }
  890. }
  891. ret=0;
  892. if (av_codec_is_decoder(avctx->codec)) {
  893. if (!avctx->bit_rate)
  894. avctx->bit_rate = get_bit_rate(avctx);
  895. /* validate channel layout from the decoder */
  896. if (avctx->channel_layout) {
  897. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  898. if (!avctx->channels)
  899. avctx->channels = channels;
  900. else if (channels != avctx->channels) {
  901. av_log(avctx, AV_LOG_WARNING,
  902. "channel layout does not match number of channels\n");
  903. avctx->channel_layout = 0;
  904. }
  905. }
  906. }
  907. end:
  908. entangled_thread_counter--;
  909. /* Release any user-supplied mutex. */
  910. if (ff_lockmgr_cb) {
  911. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  912. }
  913. if (options) {
  914. av_dict_free(options);
  915. *options = tmp;
  916. }
  917. return ret;
  918. free_and_end:
  919. av_dict_free(&tmp);
  920. av_freep(&avctx->priv_data);
  921. av_freep(&avctx->internal);
  922. avctx->codec = NULL;
  923. goto end;
  924. }
  925. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  926. {
  927. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  928. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  929. return AVERROR(EINVAL);
  930. }
  931. if (avctx) {
  932. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  933. if (!avpkt->data || avpkt->size < size) {
  934. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  935. avpkt->data = avctx->internal->byte_buffer;
  936. avpkt->size = avctx->internal->byte_buffer_size;
  937. avpkt->destruct = NULL;
  938. }
  939. }
  940. if (avpkt->data) {
  941. void *destruct = avpkt->destruct;
  942. if (avpkt->size < size) {
  943. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  944. return AVERROR(EINVAL);
  945. }
  946. av_init_packet(avpkt);
  947. avpkt->destruct = destruct;
  948. avpkt->size = size;
  949. return 0;
  950. } else {
  951. int ret = av_new_packet(avpkt, size);
  952. if (ret < 0)
  953. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  954. return ret;
  955. }
  956. }
  957. int ff_alloc_packet(AVPacket *avpkt, int size)
  958. {
  959. return ff_alloc_packet2(NULL, avpkt, size);
  960. }
  961. /**
  962. * Pad last frame with silence.
  963. */
  964. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  965. {
  966. AVFrame *frame = NULL;
  967. uint8_t *buf = NULL;
  968. int ret;
  969. if (!(frame = avcodec_alloc_frame()))
  970. return AVERROR(ENOMEM);
  971. *frame = *src;
  972. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  973. s->frame_size, s->sample_fmt, 0)) < 0)
  974. goto fail;
  975. if (!(buf = av_malloc(ret))) {
  976. ret = AVERROR(ENOMEM);
  977. goto fail;
  978. }
  979. frame->nb_samples = s->frame_size;
  980. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  981. buf, ret, 0)) < 0)
  982. goto fail;
  983. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  984. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  985. goto fail;
  986. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  987. frame->nb_samples - src->nb_samples,
  988. s->channels, s->sample_fmt)) < 0)
  989. goto fail;
  990. *dst = frame;
  991. return 0;
  992. fail:
  993. if (frame->extended_data != frame->data)
  994. av_freep(&frame->extended_data);
  995. av_freep(&buf);
  996. av_freep(&frame);
  997. return ret;
  998. }
  999. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1000. AVPacket *avpkt,
  1001. const AVFrame *frame,
  1002. int *got_packet_ptr)
  1003. {
  1004. AVFrame tmp;
  1005. AVFrame *padded_frame = NULL;
  1006. int ret;
  1007. AVPacket user_pkt = *avpkt;
  1008. int needs_realloc = !user_pkt.data;
  1009. *got_packet_ptr = 0;
  1010. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1011. av_free_packet(avpkt);
  1012. av_init_packet(avpkt);
  1013. return 0;
  1014. }
  1015. /* ensure that extended_data is properly set */
  1016. if (frame && !frame->extended_data) {
  1017. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1018. avctx->channels > AV_NUM_DATA_POINTERS) {
  1019. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1020. "with more than %d channels, but extended_data is not set.\n",
  1021. AV_NUM_DATA_POINTERS);
  1022. return AVERROR(EINVAL);
  1023. }
  1024. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1025. tmp = *frame;
  1026. tmp.extended_data = tmp.data;
  1027. frame = &tmp;
  1028. }
  1029. /* check for valid frame size */
  1030. if (frame) {
  1031. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1032. if (frame->nb_samples > avctx->frame_size) {
  1033. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1034. return AVERROR(EINVAL);
  1035. }
  1036. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1037. if (frame->nb_samples < avctx->frame_size &&
  1038. !avctx->internal->last_audio_frame) {
  1039. ret = pad_last_frame(avctx, &padded_frame, frame);
  1040. if (ret < 0)
  1041. return ret;
  1042. frame = padded_frame;
  1043. avctx->internal->last_audio_frame = 1;
  1044. }
  1045. if (frame->nb_samples != avctx->frame_size) {
  1046. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1047. ret = AVERROR(EINVAL);
  1048. goto end;
  1049. }
  1050. }
  1051. }
  1052. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1053. if (!ret) {
  1054. if (*got_packet_ptr) {
  1055. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1056. if (avpkt->pts == AV_NOPTS_VALUE)
  1057. avpkt->pts = frame->pts;
  1058. if (!avpkt->duration)
  1059. avpkt->duration = ff_samples_to_time_base(avctx,
  1060. frame->nb_samples);
  1061. }
  1062. avpkt->dts = avpkt->pts;
  1063. } else {
  1064. avpkt->size = 0;
  1065. }
  1066. }
  1067. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1068. needs_realloc = 0;
  1069. if (user_pkt.data) {
  1070. if (user_pkt.size >= avpkt->size) {
  1071. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1072. } else {
  1073. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1074. avpkt->size = user_pkt.size;
  1075. ret = -1;
  1076. }
  1077. avpkt->data = user_pkt.data;
  1078. avpkt->destruct = user_pkt.destruct;
  1079. } else {
  1080. if (av_dup_packet(avpkt) < 0) {
  1081. ret = AVERROR(ENOMEM);
  1082. }
  1083. }
  1084. }
  1085. if (!ret) {
  1086. if (needs_realloc && avpkt->data) {
  1087. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1088. if (new_data)
  1089. avpkt->data = new_data;
  1090. }
  1091. avctx->frame_number++;
  1092. }
  1093. if (ret < 0 || !*got_packet_ptr) {
  1094. av_free_packet(avpkt);
  1095. av_init_packet(avpkt);
  1096. goto end;
  1097. }
  1098. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1099. * this needs to be moved to the encoders, but for now we can do it
  1100. * here to simplify things */
  1101. avpkt->flags |= AV_PKT_FLAG_KEY;
  1102. end:
  1103. if (padded_frame) {
  1104. av_freep(&padded_frame->data[0]);
  1105. if (padded_frame->extended_data != padded_frame->data)
  1106. av_freep(&padded_frame->extended_data);
  1107. av_freep(&padded_frame);
  1108. }
  1109. return ret;
  1110. }
  1111. #if FF_API_OLD_DECODE_AUDIO
  1112. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1113. uint8_t *buf, int buf_size,
  1114. const short *samples)
  1115. {
  1116. AVPacket pkt;
  1117. AVFrame frame0;
  1118. AVFrame *frame;
  1119. int ret, samples_size, got_packet;
  1120. av_init_packet(&pkt);
  1121. pkt.data = buf;
  1122. pkt.size = buf_size;
  1123. if (samples) {
  1124. frame = &frame0;
  1125. avcodec_get_frame_defaults(frame);
  1126. if (avctx->frame_size) {
  1127. frame->nb_samples = avctx->frame_size;
  1128. } else {
  1129. /* if frame_size is not set, the number of samples must be
  1130. * calculated from the buffer size */
  1131. int64_t nb_samples;
  1132. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1133. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1134. "support this codec\n");
  1135. return AVERROR(EINVAL);
  1136. }
  1137. nb_samples = (int64_t)buf_size * 8 /
  1138. (av_get_bits_per_sample(avctx->codec_id) *
  1139. avctx->channels);
  1140. if (nb_samples >= INT_MAX)
  1141. return AVERROR(EINVAL);
  1142. frame->nb_samples = nb_samples;
  1143. }
  1144. /* it is assumed that the samples buffer is large enough based on the
  1145. * relevant parameters */
  1146. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1147. frame->nb_samples,
  1148. avctx->sample_fmt, 1);
  1149. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1150. avctx->sample_fmt,
  1151. (const uint8_t *)samples,
  1152. samples_size, 1)))
  1153. return ret;
  1154. /* fabricate frame pts from sample count.
  1155. * this is needed because the avcodec_encode_audio() API does not have
  1156. * a way for the user to provide pts */
  1157. if (avctx->sample_rate && avctx->time_base.num)
  1158. frame->pts = ff_samples_to_time_base(avctx,
  1159. avctx->internal->sample_count);
  1160. else
  1161. frame->pts = AV_NOPTS_VALUE;
  1162. avctx->internal->sample_count += frame->nb_samples;
  1163. } else {
  1164. frame = NULL;
  1165. }
  1166. got_packet = 0;
  1167. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1168. if (!ret && got_packet && avctx->coded_frame) {
  1169. avctx->coded_frame->pts = pkt.pts;
  1170. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1171. }
  1172. /* free any side data since we cannot return it */
  1173. ff_packet_free_side_data(&pkt);
  1174. if (frame && frame->extended_data != frame->data)
  1175. av_freep(&frame->extended_data);
  1176. return ret ? ret : pkt.size;
  1177. }
  1178. #endif
  1179. #if FF_API_OLD_ENCODE_VIDEO
  1180. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1181. const AVFrame *pict)
  1182. {
  1183. AVPacket pkt;
  1184. int ret, got_packet = 0;
  1185. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1186. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1187. return -1;
  1188. }
  1189. av_init_packet(&pkt);
  1190. pkt.data = buf;
  1191. pkt.size = buf_size;
  1192. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1193. if (!ret && got_packet && avctx->coded_frame) {
  1194. avctx->coded_frame->pts = pkt.pts;
  1195. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1196. }
  1197. /* free any side data since we cannot return it */
  1198. if (pkt.side_data_elems > 0) {
  1199. int i;
  1200. for (i = 0; i < pkt.side_data_elems; i++)
  1201. av_free(pkt.side_data[i].data);
  1202. av_freep(&pkt.side_data);
  1203. pkt.side_data_elems = 0;
  1204. }
  1205. return ret ? ret : pkt.size;
  1206. }
  1207. #endif
  1208. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1209. AVPacket *avpkt,
  1210. const AVFrame *frame,
  1211. int *got_packet_ptr)
  1212. {
  1213. int ret;
  1214. AVPacket user_pkt = *avpkt;
  1215. int needs_realloc = !user_pkt.data;
  1216. *got_packet_ptr = 0;
  1217. if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1218. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1219. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1220. av_free_packet(avpkt);
  1221. av_init_packet(avpkt);
  1222. avpkt->size = 0;
  1223. return 0;
  1224. }
  1225. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1226. return AVERROR(EINVAL);
  1227. av_assert0(avctx->codec->encode2);
  1228. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1229. av_assert0(ret <= 0);
  1230. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1231. needs_realloc = 0;
  1232. if (user_pkt.data) {
  1233. if (user_pkt.size >= avpkt->size) {
  1234. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1235. } else {
  1236. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1237. avpkt->size = user_pkt.size;
  1238. ret = -1;
  1239. }
  1240. avpkt->data = user_pkt.data;
  1241. avpkt->destruct = user_pkt.destruct;
  1242. } else {
  1243. if (av_dup_packet(avpkt) < 0) {
  1244. ret = AVERROR(ENOMEM);
  1245. }
  1246. }
  1247. }
  1248. if (!ret) {
  1249. if (!*got_packet_ptr)
  1250. avpkt->size = 0;
  1251. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1252. avpkt->pts = avpkt->dts = frame->pts;
  1253. if (needs_realloc && avpkt->data &&
  1254. avpkt->destruct == av_destruct_packet) {
  1255. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1256. if (new_data)
  1257. avpkt->data = new_data;
  1258. }
  1259. avctx->frame_number++;
  1260. }
  1261. if (ret < 0 || !*got_packet_ptr)
  1262. av_free_packet(avpkt);
  1263. emms_c();
  1264. return ret;
  1265. }
  1266. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1267. const AVSubtitle *sub)
  1268. {
  1269. int ret;
  1270. if (sub->start_display_time) {
  1271. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1272. return -1;
  1273. }
  1274. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1275. avctx->frame_number++;
  1276. return ret;
  1277. }
  1278. /**
  1279. * Attempt to guess proper monotonic timestamps for decoded video frames
  1280. * which might have incorrect times. Input timestamps may wrap around, in
  1281. * which case the output will as well.
  1282. *
  1283. * @param pts the pts field of the decoded AVPacket, as passed through
  1284. * AVFrame.pkt_pts
  1285. * @param dts the dts field of the decoded AVPacket
  1286. * @return one of the input values, may be AV_NOPTS_VALUE
  1287. */
  1288. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1289. int64_t reordered_pts, int64_t dts)
  1290. {
  1291. int64_t pts = AV_NOPTS_VALUE;
  1292. if (dts != AV_NOPTS_VALUE) {
  1293. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1294. ctx->pts_correction_last_dts = dts;
  1295. }
  1296. if (reordered_pts != AV_NOPTS_VALUE) {
  1297. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1298. ctx->pts_correction_last_pts = reordered_pts;
  1299. }
  1300. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1301. && reordered_pts != AV_NOPTS_VALUE)
  1302. pts = reordered_pts;
  1303. else
  1304. pts = dts;
  1305. return pts;
  1306. }
  1307. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1308. {
  1309. int size = 0;
  1310. const uint8_t *data;
  1311. uint32_t flags;
  1312. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1313. return;
  1314. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1315. if (!data || size < 4)
  1316. return;
  1317. flags = bytestream_get_le32(&data);
  1318. size -= 4;
  1319. if (size < 4) /* Required for any of the changes */
  1320. return;
  1321. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1322. avctx->channels = bytestream_get_le32(&data);
  1323. size -= 4;
  1324. }
  1325. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1326. if (size < 8)
  1327. return;
  1328. avctx->channel_layout = bytestream_get_le64(&data);
  1329. size -= 8;
  1330. }
  1331. if (size < 4)
  1332. return;
  1333. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1334. avctx->sample_rate = bytestream_get_le32(&data);
  1335. size -= 4;
  1336. }
  1337. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1338. if (size < 8)
  1339. return;
  1340. avctx->width = bytestream_get_le32(&data);
  1341. avctx->height = bytestream_get_le32(&data);
  1342. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1343. size -= 8;
  1344. }
  1345. }
  1346. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1347. int *got_picture_ptr,
  1348. const AVPacket *avpkt)
  1349. {
  1350. int ret;
  1351. // copy to ensure we do not change avpkt
  1352. AVPacket tmp = *avpkt;
  1353. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1354. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1355. return AVERROR(EINVAL);
  1356. }
  1357. *got_picture_ptr = 0;
  1358. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1359. return AVERROR(EINVAL);
  1360. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1361. int did_split = av_packet_split_side_data(&tmp);
  1362. apply_param_change(avctx, &tmp);
  1363. avctx->pkt = &tmp;
  1364. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1365. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1366. &tmp);
  1367. else {
  1368. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1369. &tmp);
  1370. picture->pkt_dts = avpkt->dts;
  1371. if(!avctx->has_b_frames){
  1372. picture->pkt_pos = avpkt->pos;
  1373. }
  1374. //FIXME these should be under if(!avctx->has_b_frames)
  1375. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1376. if (!picture->width) picture->width = avctx->width;
  1377. if (!picture->height) picture->height = avctx->height;
  1378. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1379. }
  1380. emms_c(); //needed to avoid an emms_c() call before every return;
  1381. avctx->pkt = NULL;
  1382. if (did_split) {
  1383. ff_packet_free_side_data(&tmp);
  1384. if(ret == tmp.size)
  1385. ret = avpkt->size;
  1386. }
  1387. if (*got_picture_ptr){
  1388. avctx->frame_number++;
  1389. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1390. picture->pkt_pts,
  1391. picture->pkt_dts);
  1392. }
  1393. } else
  1394. ret = 0;
  1395. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1396. * make sure it's set correctly */
  1397. picture->extended_data = picture->data;
  1398. return ret;
  1399. }
  1400. #if FF_API_OLD_DECODE_AUDIO
  1401. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1402. int *frame_size_ptr,
  1403. AVPacket *avpkt)
  1404. {
  1405. AVFrame frame;
  1406. int ret, got_frame = 0;
  1407. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1408. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1409. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1410. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1411. "avcodec_decode_audio4()\n");
  1412. avctx->get_buffer = avcodec_default_get_buffer;
  1413. avctx->release_buffer = avcodec_default_release_buffer;
  1414. }
  1415. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1416. if (ret >= 0 && got_frame) {
  1417. int ch, plane_size;
  1418. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1419. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1420. frame.nb_samples,
  1421. avctx->sample_fmt, 1);
  1422. if (*frame_size_ptr < data_size) {
  1423. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1424. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1425. return AVERROR(EINVAL);
  1426. }
  1427. memcpy(samples, frame.extended_data[0], plane_size);
  1428. if (planar && avctx->channels > 1) {
  1429. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1430. for (ch = 1; ch < avctx->channels; ch++) {
  1431. memcpy(out, frame.extended_data[ch], plane_size);
  1432. out += plane_size;
  1433. }
  1434. }
  1435. *frame_size_ptr = data_size;
  1436. } else {
  1437. *frame_size_ptr = 0;
  1438. }
  1439. return ret;
  1440. }
  1441. #endif
  1442. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1443. AVFrame *frame,
  1444. int *got_frame_ptr,
  1445. const AVPacket *avpkt)
  1446. {
  1447. int planar, channels;
  1448. int ret = 0;
  1449. *got_frame_ptr = 0;
  1450. if (!avpkt->data && avpkt->size) {
  1451. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1452. return AVERROR(EINVAL);
  1453. }
  1454. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1455. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1456. return AVERROR(EINVAL);
  1457. }
  1458. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1459. uint8_t *side;
  1460. int side_size;
  1461. // copy to ensure we do not change avpkt
  1462. AVPacket tmp = *avpkt;
  1463. int did_split = av_packet_split_side_data(&tmp);
  1464. apply_param_change(avctx, &tmp);
  1465. avctx->pkt = &tmp;
  1466. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1467. if (ret >= 0 && *got_frame_ptr) {
  1468. avctx->frame_number++;
  1469. frame->pkt_dts = avpkt->dts;
  1470. frame->best_effort_timestamp = guess_correct_pts(avctx,
  1471. frame->pkt_pts,
  1472. frame->pkt_dts);
  1473. if (frame->format == AV_SAMPLE_FMT_NONE)
  1474. frame->format = avctx->sample_fmt;
  1475. if (!frame->channel_layout)
  1476. frame->channel_layout = avctx->channel_layout;
  1477. if (!frame->channels)
  1478. frame->channels = avctx->channels;
  1479. if (!frame->sample_rate)
  1480. frame->sample_rate = avctx->sample_rate;
  1481. }
  1482. side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1483. if(side && side_size>=10) {
  1484. avctx->internal->skip_samples = AV_RL32(side);
  1485. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1486. avctx->internal->skip_samples);
  1487. }
  1488. if (avctx->internal->skip_samples) {
  1489. if(frame->nb_samples <= avctx->internal->skip_samples){
  1490. *got_frame_ptr = 0;
  1491. avctx->internal->skip_samples -= frame->nb_samples;
  1492. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1493. avctx->internal->skip_samples);
  1494. } else {
  1495. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1496. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1497. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1498. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1499. (AVRational){1, avctx->sample_rate},
  1500. avctx->pkt_timebase);
  1501. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1502. frame->pkt_pts += diff_ts;
  1503. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1504. frame->pkt_dts += diff_ts;
  1505. if (frame->pkt_duration >= diff_ts)
  1506. frame->pkt_duration -= diff_ts;
  1507. } else {
  1508. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1509. }
  1510. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1511. avctx->internal->skip_samples, frame->nb_samples);
  1512. frame->nb_samples -= avctx->internal->skip_samples;
  1513. avctx->internal->skip_samples = 0;
  1514. }
  1515. }
  1516. avctx->pkt = NULL;
  1517. if (did_split) {
  1518. ff_packet_free_side_data(&tmp);
  1519. if(ret == tmp.size)
  1520. ret = avpkt->size;
  1521. }
  1522. }
  1523. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1524. * make sure it's set correctly; assume decoders that actually use
  1525. * extended_data are doing it correctly */
  1526. if (*got_frame_ptr) {
  1527. planar = av_sample_fmt_is_planar(frame->format);
  1528. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1529. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1530. frame->extended_data = frame->data;
  1531. } else {
  1532. frame->extended_data = NULL;
  1533. }
  1534. return ret;
  1535. }
  1536. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1537. int *got_sub_ptr,
  1538. AVPacket *avpkt)
  1539. {
  1540. int ret;
  1541. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1542. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1543. return AVERROR(EINVAL);
  1544. }
  1545. avctx->pkt = avpkt;
  1546. *got_sub_ptr = 0;
  1547. avcodec_get_subtitle_defaults(sub);
  1548. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  1549. sub->pts = av_rescale_q(avpkt->pts,
  1550. avctx->pkt_timebase, AV_TIME_BASE_Q);
  1551. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1552. if (*got_sub_ptr)
  1553. avctx->frame_number++;
  1554. return ret;
  1555. }
  1556. void avsubtitle_free(AVSubtitle *sub)
  1557. {
  1558. int i;
  1559. for (i = 0; i < sub->num_rects; i++) {
  1560. av_freep(&sub->rects[i]->pict.data[0]);
  1561. av_freep(&sub->rects[i]->pict.data[1]);
  1562. av_freep(&sub->rects[i]->pict.data[2]);
  1563. av_freep(&sub->rects[i]->pict.data[3]);
  1564. av_freep(&sub->rects[i]->text);
  1565. av_freep(&sub->rects[i]->ass);
  1566. av_freep(&sub->rects[i]);
  1567. }
  1568. av_freep(&sub->rects);
  1569. memset(sub, 0, sizeof(AVSubtitle));
  1570. }
  1571. av_cold int avcodec_close(AVCodecContext *avctx)
  1572. {
  1573. /* If there is a user-supplied mutex locking routine, call it. */
  1574. if (ff_lockmgr_cb) {
  1575. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1576. return -1;
  1577. }
  1578. entangled_thread_counter++;
  1579. if (entangled_thread_counter != 1) {
  1580. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1581. entangled_thread_counter--;
  1582. return -1;
  1583. }
  1584. if (avcodec_is_open(avctx)) {
  1585. if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1586. entangled_thread_counter --;
  1587. ff_frame_thread_encoder_free(avctx);
  1588. entangled_thread_counter ++;
  1589. }
  1590. if (HAVE_THREADS && avctx->thread_opaque)
  1591. ff_thread_free(avctx);
  1592. if (avctx->codec && avctx->codec->close)
  1593. avctx->codec->close(avctx);
  1594. avcodec_default_free_buffers(avctx);
  1595. avctx->coded_frame = NULL;
  1596. avctx->internal->byte_buffer_size = 0;
  1597. av_freep(&avctx->internal->byte_buffer);
  1598. av_freep(&avctx->internal);
  1599. }
  1600. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1601. av_opt_free(avctx->priv_data);
  1602. av_opt_free(avctx);
  1603. av_freep(&avctx->priv_data);
  1604. if (av_codec_is_encoder(avctx->codec))
  1605. av_freep(&avctx->extradata);
  1606. avctx->codec = NULL;
  1607. avctx->active_thread_type = 0;
  1608. entangled_thread_counter--;
  1609. /* Release any user-supplied mutex. */
  1610. if (ff_lockmgr_cb) {
  1611. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1612. }
  1613. return 0;
  1614. }
  1615. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  1616. {
  1617. switch(id){
  1618. //This is for future deprecatec codec ids, its empty since
  1619. //last major bump but will fill up again over time, please don't remove it
  1620. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  1621. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  1622. default : return id;
  1623. }
  1624. }
  1625. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1626. {
  1627. AVCodec *p, *experimental = NULL;
  1628. p = first_avcodec;
  1629. id= remap_deprecated_codec_id(id);
  1630. while (p) {
  1631. if (av_codec_is_encoder(p) && p->id == id) {
  1632. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1633. experimental = p;
  1634. } else
  1635. return p;
  1636. }
  1637. p = p->next;
  1638. }
  1639. return experimental;
  1640. }
  1641. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1642. {
  1643. AVCodec *p;
  1644. if (!name)
  1645. return NULL;
  1646. p = first_avcodec;
  1647. while (p) {
  1648. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1649. return p;
  1650. p = p->next;
  1651. }
  1652. return NULL;
  1653. }
  1654. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1655. {
  1656. AVCodec *p, *experimental=NULL;
  1657. p = first_avcodec;
  1658. id= remap_deprecated_codec_id(id);
  1659. while (p) {
  1660. if (av_codec_is_decoder(p) && p->id == id) {
  1661. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1662. experimental = p;
  1663. } else
  1664. return p;
  1665. }
  1666. p = p->next;
  1667. }
  1668. return experimental;
  1669. }
  1670. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1671. {
  1672. AVCodec *p;
  1673. if (!name)
  1674. return NULL;
  1675. p = first_avcodec;
  1676. while (p) {
  1677. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1678. return p;
  1679. p = p->next;
  1680. }
  1681. return NULL;
  1682. }
  1683. const char *avcodec_get_name(enum AVCodecID id)
  1684. {
  1685. const AVCodecDescriptor *cd;
  1686. AVCodec *codec;
  1687. if (id == AV_CODEC_ID_NONE)
  1688. return "none";
  1689. cd = avcodec_descriptor_get(id);
  1690. if (cd)
  1691. return cd->name;
  1692. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1693. codec = avcodec_find_decoder(id);
  1694. if (codec)
  1695. return codec->name;
  1696. codec = avcodec_find_encoder(id);
  1697. if (codec)
  1698. return codec->name;
  1699. return "unknown_codec";
  1700. }
  1701. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1702. {
  1703. int i, len, ret = 0;
  1704. #define IS_PRINT(x) \
  1705. (((x) >= '0' && (x) <= '9') || \
  1706. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1707. ((x) == '.' || (x) == ' ' || (x) == '-'))
  1708. for (i = 0; i < 4; i++) {
  1709. len = snprintf(buf, buf_size,
  1710. IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1711. buf += len;
  1712. buf_size = buf_size > len ? buf_size - len : 0;
  1713. ret += len;
  1714. codec_tag >>= 8;
  1715. }
  1716. return ret;
  1717. }
  1718. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1719. {
  1720. const char *codec_type;
  1721. const char *codec_name;
  1722. const char *profile = NULL;
  1723. const AVCodec *p;
  1724. int bitrate;
  1725. AVRational display_aspect_ratio;
  1726. if (!buf || buf_size <= 0)
  1727. return;
  1728. codec_type = av_get_media_type_string(enc->codec_type);
  1729. codec_name = avcodec_get_name(enc->codec_id);
  1730. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1731. if (enc->codec)
  1732. p = enc->codec;
  1733. else
  1734. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1735. avcodec_find_decoder(enc->codec_id);
  1736. if (p)
  1737. profile = av_get_profile_name(p, enc->profile);
  1738. }
  1739. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1740. codec_name, enc->mb_decision ? " (hq)" : "");
  1741. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1742. if (profile)
  1743. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1744. if (enc->codec_tag) {
  1745. char tag_buf[32];
  1746. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1747. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1748. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1749. }
  1750. switch (enc->codec_type) {
  1751. case AVMEDIA_TYPE_VIDEO:
  1752. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1753. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1754. ", %s",
  1755. av_get_pix_fmt_name(enc->pix_fmt));
  1756. }
  1757. if (enc->width) {
  1758. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1759. ", %dx%d",
  1760. enc->width, enc->height);
  1761. if (enc->sample_aspect_ratio.num) {
  1762. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1763. enc->width * enc->sample_aspect_ratio.num,
  1764. enc->height * enc->sample_aspect_ratio.den,
  1765. 1024 * 1024);
  1766. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1767. " [SAR %d:%d DAR %d:%d]",
  1768. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1769. display_aspect_ratio.num, display_aspect_ratio.den);
  1770. }
  1771. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1772. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1773. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1774. ", %d/%d",
  1775. enc->time_base.num / g, enc->time_base.den / g);
  1776. }
  1777. }
  1778. if (encode) {
  1779. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1780. ", q=%d-%d", enc->qmin, enc->qmax);
  1781. }
  1782. break;
  1783. case AVMEDIA_TYPE_AUDIO:
  1784. if (enc->sample_rate) {
  1785. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1786. ", %d Hz", enc->sample_rate);
  1787. }
  1788. av_strlcat(buf, ", ", buf_size);
  1789. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1790. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1791. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1792. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1793. }
  1794. break;
  1795. default:
  1796. return;
  1797. }
  1798. if (encode) {
  1799. if (enc->flags & CODEC_FLAG_PASS1)
  1800. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1801. ", pass 1");
  1802. if (enc->flags & CODEC_FLAG_PASS2)
  1803. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1804. ", pass 2");
  1805. }
  1806. bitrate = get_bit_rate(enc);
  1807. if (bitrate != 0) {
  1808. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1809. ", %d kb/s", bitrate / 1000);
  1810. }
  1811. }
  1812. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1813. {
  1814. const AVProfile *p;
  1815. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1816. return NULL;
  1817. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1818. if (p->profile == profile)
  1819. return p->name;
  1820. return NULL;
  1821. }
  1822. unsigned avcodec_version(void)
  1823. {
  1824. // av_assert0(AV_CODEC_ID_V410==164);
  1825. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  1826. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  1827. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  1828. av_assert0(AV_CODEC_ID_SRT==94216);
  1829. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1830. return LIBAVCODEC_VERSION_INT;
  1831. }
  1832. const char *avcodec_configuration(void)
  1833. {
  1834. return FFMPEG_CONFIGURATION;
  1835. }
  1836. const char *avcodec_license(void)
  1837. {
  1838. #define LICENSE_PREFIX "libavcodec license: "
  1839. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1840. }
  1841. void avcodec_flush_buffers(AVCodecContext *avctx)
  1842. {
  1843. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1844. ff_thread_flush(avctx);
  1845. else if (avctx->codec->flush)
  1846. avctx->codec->flush(avctx);
  1847. avctx->pts_correction_last_pts =
  1848. avctx->pts_correction_last_dts = INT64_MIN;
  1849. }
  1850. static void video_free_buffers(AVCodecContext *s)
  1851. {
  1852. AVCodecInternal *avci = s->internal;
  1853. int i, j;
  1854. if (!avci->buffer)
  1855. return;
  1856. if (avci->buffer_count)
  1857. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1858. avci->buffer_count);
  1859. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  1860. InternalBuffer *buf = &avci->buffer[i];
  1861. for (j = 0; j < 4; j++) {
  1862. av_freep(&buf->base[j]);
  1863. buf->data[j] = NULL;
  1864. }
  1865. }
  1866. av_freep(&avci->buffer);
  1867. avci->buffer_count = 0;
  1868. }
  1869. static void audio_free_buffers(AVCodecContext *avctx)
  1870. {
  1871. AVCodecInternal *avci = avctx->internal;
  1872. InternalBuffer *buf;
  1873. if (!avci->buffer)
  1874. return;
  1875. buf = avci->buffer;
  1876. if (buf->extended_data) {
  1877. av_free(buf->extended_data[0]);
  1878. if (buf->extended_data != buf->data)
  1879. av_freep(&buf->extended_data);
  1880. }
  1881. av_freep(&avci->buffer);
  1882. }
  1883. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1884. {
  1885. switch (avctx->codec_type) {
  1886. case AVMEDIA_TYPE_VIDEO:
  1887. video_free_buffers(avctx);
  1888. break;
  1889. case AVMEDIA_TYPE_AUDIO:
  1890. audio_free_buffers(avctx);
  1891. break;
  1892. default:
  1893. break;
  1894. }
  1895. }
  1896. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1897. {
  1898. switch (codec_id) {
  1899. case AV_CODEC_ID_ADPCM_CT:
  1900. case AV_CODEC_ID_ADPCM_IMA_APC:
  1901. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1902. case AV_CODEC_ID_ADPCM_IMA_WS:
  1903. case AV_CODEC_ID_ADPCM_G722:
  1904. case AV_CODEC_ID_ADPCM_YAMAHA:
  1905. return 4;
  1906. case AV_CODEC_ID_PCM_ALAW:
  1907. case AV_CODEC_ID_PCM_MULAW:
  1908. case AV_CODEC_ID_PCM_S8:
  1909. case AV_CODEC_ID_PCM_U8:
  1910. case AV_CODEC_ID_PCM_ZORK:
  1911. return 8;
  1912. case AV_CODEC_ID_PCM_S16BE:
  1913. case AV_CODEC_ID_PCM_S16LE:
  1914. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1915. case AV_CODEC_ID_PCM_U16BE:
  1916. case AV_CODEC_ID_PCM_U16LE:
  1917. return 16;
  1918. case AV_CODEC_ID_PCM_S24DAUD:
  1919. case AV_CODEC_ID_PCM_S24BE:
  1920. case AV_CODEC_ID_PCM_S24LE:
  1921. case AV_CODEC_ID_PCM_U24BE:
  1922. case AV_CODEC_ID_PCM_U24LE:
  1923. return 24;
  1924. case AV_CODEC_ID_PCM_S32BE:
  1925. case AV_CODEC_ID_PCM_S32LE:
  1926. case AV_CODEC_ID_PCM_U32BE:
  1927. case AV_CODEC_ID_PCM_U32LE:
  1928. case AV_CODEC_ID_PCM_F32BE:
  1929. case AV_CODEC_ID_PCM_F32LE:
  1930. return 32;
  1931. case AV_CODEC_ID_PCM_F64BE:
  1932. case AV_CODEC_ID_PCM_F64LE:
  1933. return 64;
  1934. default:
  1935. return 0;
  1936. }
  1937. }
  1938. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1939. {
  1940. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  1941. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  1942. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  1943. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  1944. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  1945. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  1946. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  1947. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  1948. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  1949. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  1950. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  1951. };
  1952. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  1953. return AV_CODEC_ID_NONE;
  1954. if (be < 0 || be > 1)
  1955. be = AV_NE(1, 0);
  1956. return map[fmt][be];
  1957. }
  1958. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1959. {
  1960. switch (codec_id) {
  1961. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1962. return 2;
  1963. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1964. return 3;
  1965. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1966. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1967. case AV_CODEC_ID_ADPCM_IMA_QT:
  1968. case AV_CODEC_ID_ADPCM_SWF:
  1969. case AV_CODEC_ID_ADPCM_MS:
  1970. return 4;
  1971. default:
  1972. return av_get_exact_bits_per_sample(codec_id);
  1973. }
  1974. }
  1975. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1976. {
  1977. int id, sr, ch, ba, tag, bps;
  1978. id = avctx->codec_id;
  1979. sr = avctx->sample_rate;
  1980. ch = avctx->channels;
  1981. ba = avctx->block_align;
  1982. tag = avctx->codec_tag;
  1983. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1984. /* codecs with an exact constant bits per sample */
  1985. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  1986. return (frame_bytes * 8LL) / (bps * ch);
  1987. bps = avctx->bits_per_coded_sample;
  1988. /* codecs with a fixed packet duration */
  1989. switch (id) {
  1990. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1991. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1992. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1993. case AV_CODEC_ID_AMR_NB:
  1994. case AV_CODEC_ID_GSM:
  1995. case AV_CODEC_ID_QCELP:
  1996. case AV_CODEC_ID_RA_288: return 160;
  1997. case AV_CODEC_ID_IMC: return 256;
  1998. case AV_CODEC_ID_AMR_WB:
  1999. case AV_CODEC_ID_GSM_MS: return 320;
  2000. case AV_CODEC_ID_MP1: return 384;
  2001. case AV_CODEC_ID_ATRAC1: return 512;
  2002. case AV_CODEC_ID_ATRAC3: return 1024;
  2003. case AV_CODEC_ID_MP2:
  2004. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2005. case AV_CODEC_ID_AC3: return 1536;
  2006. }
  2007. if (sr > 0) {
  2008. /* calc from sample rate */
  2009. if (id == AV_CODEC_ID_TTA)
  2010. return 256 * sr / 245;
  2011. if (ch > 0) {
  2012. /* calc from sample rate and channels */
  2013. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2014. return (480 << (sr / 22050)) / ch;
  2015. }
  2016. }
  2017. if (ba > 0) {
  2018. /* calc from block_align */
  2019. if (id == AV_CODEC_ID_SIPR) {
  2020. switch (ba) {
  2021. case 20: return 160;
  2022. case 19: return 144;
  2023. case 29: return 288;
  2024. case 37: return 480;
  2025. }
  2026. } else if (id == AV_CODEC_ID_ILBC) {
  2027. switch (ba) {
  2028. case 38: return 160;
  2029. case 50: return 240;
  2030. }
  2031. }
  2032. }
  2033. if (frame_bytes > 0) {
  2034. /* calc from frame_bytes only */
  2035. if (id == AV_CODEC_ID_TRUESPEECH)
  2036. return 240 * (frame_bytes / 32);
  2037. if (id == AV_CODEC_ID_NELLYMOSER)
  2038. return 256 * (frame_bytes / 64);
  2039. if (id == AV_CODEC_ID_RA_144)
  2040. return 160 * (frame_bytes / 20);
  2041. if (bps > 0) {
  2042. /* calc from frame_bytes and bits_per_coded_sample */
  2043. if (id == AV_CODEC_ID_ADPCM_G726)
  2044. return frame_bytes * 8 / bps;
  2045. }
  2046. if (ch > 0) {
  2047. /* calc from frame_bytes and channels */
  2048. switch (id) {
  2049. case AV_CODEC_ID_ADPCM_4XM:
  2050. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2051. return (frame_bytes - 4 * ch) * 2 / ch;
  2052. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2053. return (frame_bytes - 4) * 2 / ch;
  2054. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2055. return (frame_bytes - 8) * 2 / ch;
  2056. case AV_CODEC_ID_ADPCM_XA:
  2057. return (frame_bytes / 128) * 224 / ch;
  2058. case AV_CODEC_ID_INTERPLAY_DPCM:
  2059. return (frame_bytes - 6 - ch) / ch;
  2060. case AV_CODEC_ID_ROQ_DPCM:
  2061. return (frame_bytes - 8) / ch;
  2062. case AV_CODEC_ID_XAN_DPCM:
  2063. return (frame_bytes - 2 * ch) / ch;
  2064. case AV_CODEC_ID_MACE3:
  2065. return 3 * frame_bytes / ch;
  2066. case AV_CODEC_ID_MACE6:
  2067. return 6 * frame_bytes / ch;
  2068. case AV_CODEC_ID_PCM_LXF:
  2069. return 2 * (frame_bytes / (5 * ch));
  2070. }
  2071. if (tag) {
  2072. /* calc from frame_bytes, channels, and codec_tag */
  2073. if (id == AV_CODEC_ID_SOL_DPCM) {
  2074. if (tag == 3)
  2075. return frame_bytes / ch;
  2076. else
  2077. return frame_bytes * 2 / ch;
  2078. }
  2079. }
  2080. if (ba > 0) {
  2081. /* calc from frame_bytes, channels, and block_align */
  2082. int blocks = frame_bytes / ba;
  2083. switch (avctx->codec_id) {
  2084. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2085. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2086. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2087. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2088. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2089. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2090. case AV_CODEC_ID_ADPCM_MS:
  2091. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2092. }
  2093. }
  2094. if (bps > 0) {
  2095. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2096. switch (avctx->codec_id) {
  2097. case AV_CODEC_ID_PCM_DVD:
  2098. if(bps<4)
  2099. return 0;
  2100. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2101. case AV_CODEC_ID_PCM_BLURAY:
  2102. if(bps<4)
  2103. return 0;
  2104. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2105. case AV_CODEC_ID_S302M:
  2106. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2107. }
  2108. }
  2109. }
  2110. }
  2111. return 0;
  2112. }
  2113. #if !HAVE_THREADS
  2114. int ff_thread_init(AVCodecContext *s)
  2115. {
  2116. return -1;
  2117. }
  2118. #endif
  2119. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2120. {
  2121. unsigned int n = 0;
  2122. while (v >= 0xff) {
  2123. *s++ = 0xff;
  2124. v -= 0xff;
  2125. n++;
  2126. }
  2127. *s = v;
  2128. n++;
  2129. return n;
  2130. }
  2131. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2132. {
  2133. int i;
  2134. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2135. return i;
  2136. }
  2137. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2138. {
  2139. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2140. "version to the newest one from Git. If the problem still "
  2141. "occurs, it means that your file has a feature which has not "
  2142. "been implemented.\n", feature);
  2143. if(want_sample)
  2144. av_log_ask_for_sample(avc, NULL);
  2145. }
  2146. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2147. {
  2148. va_list argument_list;
  2149. va_start(argument_list, msg);
  2150. if (msg)
  2151. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2152. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2153. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2154. "and contact the ffmpeg-devel mailing list.\n");
  2155. va_end(argument_list);
  2156. }
  2157. static AVHWAccel *first_hwaccel = NULL;
  2158. void av_register_hwaccel(AVHWAccel *hwaccel)
  2159. {
  2160. AVHWAccel **p = &first_hwaccel;
  2161. while (*p)
  2162. p = &(*p)->next;
  2163. *p = hwaccel;
  2164. hwaccel->next = NULL;
  2165. }
  2166. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2167. {
  2168. return hwaccel ? hwaccel->next : first_hwaccel;
  2169. }
  2170. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  2171. {
  2172. AVHWAccel *hwaccel = NULL;
  2173. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2174. if (hwaccel->id == codec_id
  2175. && hwaccel->pix_fmt == pix_fmt)
  2176. return hwaccel;
  2177. return NULL;
  2178. }
  2179. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2180. {
  2181. if (ff_lockmgr_cb) {
  2182. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2183. return -1;
  2184. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2185. return -1;
  2186. }
  2187. ff_lockmgr_cb = cb;
  2188. if (ff_lockmgr_cb) {
  2189. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2190. return -1;
  2191. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2192. return -1;
  2193. }
  2194. return 0;
  2195. }
  2196. int avpriv_lock_avformat(void)
  2197. {
  2198. if (ff_lockmgr_cb) {
  2199. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2200. return -1;
  2201. }
  2202. return 0;
  2203. }
  2204. int avpriv_unlock_avformat(void)
  2205. {
  2206. if (ff_lockmgr_cb) {
  2207. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2208. return -1;
  2209. }
  2210. return 0;
  2211. }
  2212. unsigned int avpriv_toupper4(unsigned int x)
  2213. {
  2214. return toupper(x & 0xFF)
  2215. + (toupper((x >> 8) & 0xFF) << 8)
  2216. + (toupper((x >> 16) & 0xFF) << 16)
  2217. + (toupper((x >> 24) & 0xFF) << 24);
  2218. }
  2219. #if !HAVE_THREADS
  2220. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2221. {
  2222. f->owner = avctx;
  2223. ff_init_buffer_info(avctx, f);
  2224. return avctx->get_buffer(avctx, f);
  2225. }
  2226. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2227. {
  2228. f->owner->release_buffer(f->owner, f);
  2229. }
  2230. void ff_thread_finish_setup(AVCodecContext *avctx)
  2231. {
  2232. }
  2233. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2234. {
  2235. }
  2236. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2237. {
  2238. }
  2239. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2240. {
  2241. return 1;
  2242. }
  2243. #endif
  2244. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2245. {
  2246. AVCodec *c= avcodec_find_decoder(codec_id);
  2247. if(!c)
  2248. c= avcodec_find_encoder(codec_id);
  2249. if(c)
  2250. return c->type;
  2251. if (codec_id <= AV_CODEC_ID_NONE)
  2252. return AVMEDIA_TYPE_UNKNOWN;
  2253. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2254. return AVMEDIA_TYPE_VIDEO;
  2255. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2256. return AVMEDIA_TYPE_AUDIO;
  2257. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2258. return AVMEDIA_TYPE_SUBTITLE;
  2259. return AVMEDIA_TYPE_UNKNOWN;
  2260. }
  2261. int avcodec_is_open(AVCodecContext *s)
  2262. {
  2263. return !!s->internal;
  2264. }