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.

2580 lines
82KB

  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 PIX_FMT_YUV420P:
  162. case PIX_FMT_YUYV422:
  163. case PIX_FMT_UYVY422:
  164. case PIX_FMT_YUV422P:
  165. case PIX_FMT_YUV440P:
  166. case PIX_FMT_YUV444P:
  167. case PIX_FMT_GBRP:
  168. case PIX_FMT_GRAY8:
  169. case PIX_FMT_GRAY16BE:
  170. case PIX_FMT_GRAY16LE:
  171. case PIX_FMT_YUVJ420P:
  172. case PIX_FMT_YUVJ422P:
  173. case PIX_FMT_YUVJ440P:
  174. case PIX_FMT_YUVJ444P:
  175. case PIX_FMT_YUVA420P:
  176. case PIX_FMT_YUVA422P:
  177. case PIX_FMT_YUVA444P:
  178. case PIX_FMT_YUV420P9LE:
  179. case PIX_FMT_YUV420P9BE:
  180. case PIX_FMT_YUV420P10LE:
  181. case PIX_FMT_YUV420P10BE:
  182. case PIX_FMT_YUV420P12LE:
  183. case PIX_FMT_YUV420P12BE:
  184. case PIX_FMT_YUV420P14LE:
  185. case PIX_FMT_YUV420P14BE:
  186. case PIX_FMT_YUV422P9LE:
  187. case PIX_FMT_YUV422P9BE:
  188. case PIX_FMT_YUV422P10LE:
  189. case PIX_FMT_YUV422P10BE:
  190. case PIX_FMT_YUV422P12LE:
  191. case PIX_FMT_YUV422P12BE:
  192. case PIX_FMT_YUV422P14LE:
  193. case PIX_FMT_YUV422P14BE:
  194. case PIX_FMT_YUV444P9LE:
  195. case PIX_FMT_YUV444P9BE:
  196. case PIX_FMT_YUV444P10LE:
  197. case PIX_FMT_YUV444P10BE:
  198. case PIX_FMT_YUV444P12LE:
  199. case PIX_FMT_YUV444P12BE:
  200. case PIX_FMT_YUV444P14LE:
  201. case PIX_FMT_YUV444P14BE:
  202. case PIX_FMT_GBRP9LE:
  203. case PIX_FMT_GBRP9BE:
  204. case PIX_FMT_GBRP10LE:
  205. case PIX_FMT_GBRP10BE:
  206. case PIX_FMT_GBRP12LE:
  207. case PIX_FMT_GBRP12BE:
  208. case PIX_FMT_GBRP14LE:
  209. case 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 PIX_FMT_YUV411P:
  214. case PIX_FMT_UYYVYY411:
  215. w_align = 32;
  216. h_align = 8;
  217. break;
  218. case PIX_FMT_YUV410P:
  219. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  220. w_align = 64;
  221. h_align = 64;
  222. }
  223. case PIX_FMT_RGB555:
  224. if (s->codec_id == AV_CODEC_ID_RPZA) {
  225. w_align = 4;
  226. h_align = 4;
  227. }
  228. case PIX_FMT_PAL8:
  229. case PIX_FMT_BGR8:
  230. case 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 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_freep(&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 PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt)
  599. {
  600. while (*fmt != 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->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  822. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  823. ret = AVERROR(EINVAL);
  824. goto free_and_end;
  825. }
  826. }
  827. if (avctx->codec->pix_fmts) {
  828. for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
  829. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  830. break;
  831. if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE
  832. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  833. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  834. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  835. ret = AVERROR(EINVAL);
  836. goto free_and_end;
  837. }
  838. }
  839. if (avctx->codec->supported_samplerates) {
  840. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  841. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  842. break;
  843. if (avctx->codec->supported_samplerates[i] == 0) {
  844. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  845. ret = AVERROR(EINVAL);
  846. goto free_and_end;
  847. }
  848. }
  849. if (avctx->codec->channel_layouts) {
  850. if (!avctx->channel_layout) {
  851. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  852. } else {
  853. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  854. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  855. break;
  856. if (avctx->codec->channel_layouts[i] == 0) {
  857. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  858. ret = AVERROR(EINVAL);
  859. goto free_and_end;
  860. }
  861. }
  862. }
  863. if (avctx->channel_layout && avctx->channels) {
  864. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  865. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  866. ret = AVERROR(EINVAL);
  867. goto free_and_end;
  868. }
  869. } else if (avctx->channel_layout) {
  870. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  871. }
  872. }
  873. avctx->pts_correction_num_faulty_pts =
  874. avctx->pts_correction_num_faulty_dts = 0;
  875. avctx->pts_correction_last_pts =
  876. avctx->pts_correction_last_dts = INT64_MIN;
  877. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  878. || avctx->internal->frame_thread_encoder)) {
  879. ret = avctx->codec->init(avctx);
  880. if (ret < 0) {
  881. goto free_and_end;
  882. }
  883. }
  884. ret=0;
  885. if (av_codec_is_decoder(avctx->codec)) {
  886. if (!avctx->bit_rate)
  887. avctx->bit_rate = get_bit_rate(avctx);
  888. /* validate channel layout from the decoder */
  889. if (avctx->channel_layout) {
  890. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  891. if (!avctx->channels)
  892. avctx->channels = channels;
  893. else if (channels != avctx->channels) {
  894. av_log(avctx, AV_LOG_WARNING,
  895. "channel layout does not match number of channels\n");
  896. avctx->channel_layout = 0;
  897. }
  898. }
  899. }
  900. end:
  901. entangled_thread_counter--;
  902. /* Release any user-supplied mutex. */
  903. if (ff_lockmgr_cb) {
  904. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  905. }
  906. if (options) {
  907. av_dict_free(options);
  908. *options = tmp;
  909. }
  910. return ret;
  911. free_and_end:
  912. av_dict_free(&tmp);
  913. av_freep(&avctx->priv_data);
  914. av_freep(&avctx->internal);
  915. avctx->codec = NULL;
  916. goto end;
  917. }
  918. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  919. {
  920. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  921. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  922. return AVERROR(EINVAL);
  923. }
  924. if (avctx) {
  925. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  926. if (!avpkt->data || avpkt->size < size) {
  927. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  928. avpkt->data = avctx->internal->byte_buffer;
  929. avpkt->size = avctx->internal->byte_buffer_size;
  930. avpkt->destruct = NULL;
  931. }
  932. }
  933. if (avpkt->data) {
  934. void *destruct = avpkt->destruct;
  935. if (avpkt->size < size) {
  936. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  937. return AVERROR(EINVAL);
  938. }
  939. av_init_packet(avpkt);
  940. avpkt->destruct = destruct;
  941. avpkt->size = size;
  942. return 0;
  943. } else {
  944. int ret = av_new_packet(avpkt, size);
  945. if (ret < 0)
  946. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  947. return ret;
  948. }
  949. }
  950. int ff_alloc_packet(AVPacket *avpkt, int size)
  951. {
  952. return ff_alloc_packet2(NULL, avpkt, size);
  953. }
  954. /**
  955. * Pad last frame with silence.
  956. */
  957. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  958. {
  959. AVFrame *frame = NULL;
  960. uint8_t *buf = NULL;
  961. int ret;
  962. if (!(frame = avcodec_alloc_frame()))
  963. return AVERROR(ENOMEM);
  964. *frame = *src;
  965. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  966. s->frame_size, s->sample_fmt, 0)) < 0)
  967. goto fail;
  968. if (!(buf = av_malloc(ret))) {
  969. ret = AVERROR(ENOMEM);
  970. goto fail;
  971. }
  972. frame->nb_samples = s->frame_size;
  973. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  974. buf, ret, 0)) < 0)
  975. goto fail;
  976. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  977. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  978. goto fail;
  979. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  980. frame->nb_samples - src->nb_samples,
  981. s->channels, s->sample_fmt)) < 0)
  982. goto fail;
  983. *dst = frame;
  984. return 0;
  985. fail:
  986. if (frame->extended_data != frame->data)
  987. av_freep(&frame->extended_data);
  988. av_freep(&buf);
  989. av_freep(&frame);
  990. return ret;
  991. }
  992. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  993. AVPacket *avpkt,
  994. const AVFrame *frame,
  995. int *got_packet_ptr)
  996. {
  997. AVFrame tmp;
  998. AVFrame *padded_frame = NULL;
  999. int ret;
  1000. AVPacket user_pkt = *avpkt;
  1001. int needs_realloc = !user_pkt.data;
  1002. *got_packet_ptr = 0;
  1003. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1004. av_free_packet(avpkt);
  1005. av_init_packet(avpkt);
  1006. return 0;
  1007. }
  1008. /* ensure that extended_data is properly set */
  1009. if (frame && !frame->extended_data) {
  1010. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1011. avctx->channels > AV_NUM_DATA_POINTERS) {
  1012. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1013. "with more than %d channels, but extended_data is not set.\n",
  1014. AV_NUM_DATA_POINTERS);
  1015. return AVERROR(EINVAL);
  1016. }
  1017. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1018. tmp = *frame;
  1019. tmp.extended_data = tmp.data;
  1020. frame = &tmp;
  1021. }
  1022. /* check for valid frame size */
  1023. if (frame) {
  1024. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1025. if (frame->nb_samples > avctx->frame_size) {
  1026. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1027. return AVERROR(EINVAL);
  1028. }
  1029. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1030. if (frame->nb_samples < avctx->frame_size &&
  1031. !avctx->internal->last_audio_frame) {
  1032. ret = pad_last_frame(avctx, &padded_frame, frame);
  1033. if (ret < 0)
  1034. return ret;
  1035. frame = padded_frame;
  1036. avctx->internal->last_audio_frame = 1;
  1037. }
  1038. if (frame->nb_samples != avctx->frame_size) {
  1039. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1040. ret = AVERROR(EINVAL);
  1041. goto end;
  1042. }
  1043. }
  1044. }
  1045. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1046. if (!ret) {
  1047. if (*got_packet_ptr) {
  1048. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1049. if (avpkt->pts == AV_NOPTS_VALUE)
  1050. avpkt->pts = frame->pts;
  1051. if (!avpkt->duration)
  1052. avpkt->duration = ff_samples_to_time_base(avctx,
  1053. frame->nb_samples);
  1054. }
  1055. avpkt->dts = avpkt->pts;
  1056. } else {
  1057. avpkt->size = 0;
  1058. }
  1059. }
  1060. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1061. needs_realloc = 0;
  1062. if (user_pkt.data) {
  1063. if (user_pkt.size >= avpkt->size) {
  1064. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1065. } else {
  1066. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1067. avpkt->size = user_pkt.size;
  1068. ret = -1;
  1069. }
  1070. avpkt->data = user_pkt.data;
  1071. avpkt->destruct = user_pkt.destruct;
  1072. } else {
  1073. if (av_dup_packet(avpkt) < 0) {
  1074. ret = AVERROR(ENOMEM);
  1075. }
  1076. }
  1077. }
  1078. if (!ret) {
  1079. if (needs_realloc && avpkt->data) {
  1080. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1081. if (new_data)
  1082. avpkt->data = new_data;
  1083. }
  1084. avctx->frame_number++;
  1085. }
  1086. if (ret < 0 || !*got_packet_ptr) {
  1087. av_free_packet(avpkt);
  1088. av_init_packet(avpkt);
  1089. goto end;
  1090. }
  1091. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1092. * this needs to be moved to the encoders, but for now we can do it
  1093. * here to simplify things */
  1094. avpkt->flags |= AV_PKT_FLAG_KEY;
  1095. end:
  1096. if (padded_frame) {
  1097. av_freep(&padded_frame->data[0]);
  1098. if (padded_frame->extended_data != padded_frame->data)
  1099. av_freep(&padded_frame->extended_data);
  1100. av_freep(&padded_frame);
  1101. }
  1102. return ret;
  1103. }
  1104. #if FF_API_OLD_DECODE_AUDIO
  1105. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1106. uint8_t *buf, int buf_size,
  1107. const short *samples)
  1108. {
  1109. AVPacket pkt;
  1110. AVFrame frame0;
  1111. AVFrame *frame;
  1112. int ret, samples_size, got_packet;
  1113. av_init_packet(&pkt);
  1114. pkt.data = buf;
  1115. pkt.size = buf_size;
  1116. if (samples) {
  1117. frame = &frame0;
  1118. avcodec_get_frame_defaults(frame);
  1119. if (avctx->frame_size) {
  1120. frame->nb_samples = avctx->frame_size;
  1121. } else {
  1122. /* if frame_size is not set, the number of samples must be
  1123. * calculated from the buffer size */
  1124. int64_t nb_samples;
  1125. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1126. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1127. "support this codec\n");
  1128. return AVERROR(EINVAL);
  1129. }
  1130. nb_samples = (int64_t)buf_size * 8 /
  1131. (av_get_bits_per_sample(avctx->codec_id) *
  1132. avctx->channels);
  1133. if (nb_samples >= INT_MAX)
  1134. return AVERROR(EINVAL);
  1135. frame->nb_samples = nb_samples;
  1136. }
  1137. /* it is assumed that the samples buffer is large enough based on the
  1138. * relevant parameters */
  1139. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1140. frame->nb_samples,
  1141. avctx->sample_fmt, 1);
  1142. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1143. avctx->sample_fmt,
  1144. (const uint8_t *)samples,
  1145. samples_size, 1)))
  1146. return ret;
  1147. /* fabricate frame pts from sample count.
  1148. * this is needed because the avcodec_encode_audio() API does not have
  1149. * a way for the user to provide pts */
  1150. if (avctx->sample_rate && avctx->time_base.num)
  1151. frame->pts = ff_samples_to_time_base(avctx,
  1152. avctx->internal->sample_count);
  1153. else
  1154. frame->pts = AV_NOPTS_VALUE;
  1155. avctx->internal->sample_count += frame->nb_samples;
  1156. } else {
  1157. frame = NULL;
  1158. }
  1159. got_packet = 0;
  1160. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1161. if (!ret && got_packet && avctx->coded_frame) {
  1162. avctx->coded_frame->pts = pkt.pts;
  1163. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1164. }
  1165. /* free any side data since we cannot return it */
  1166. ff_packet_free_side_data(&pkt);
  1167. if (frame && frame->extended_data != frame->data)
  1168. av_freep(&frame->extended_data);
  1169. return ret ? ret : pkt.size;
  1170. }
  1171. #endif
  1172. #if FF_API_OLD_ENCODE_VIDEO
  1173. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1174. const AVFrame *pict)
  1175. {
  1176. AVPacket pkt;
  1177. int ret, got_packet = 0;
  1178. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1179. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1180. return -1;
  1181. }
  1182. av_init_packet(&pkt);
  1183. pkt.data = buf;
  1184. pkt.size = buf_size;
  1185. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1186. if (!ret && got_packet && avctx->coded_frame) {
  1187. avctx->coded_frame->pts = pkt.pts;
  1188. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1189. }
  1190. /* free any side data since we cannot return it */
  1191. if (pkt.side_data_elems > 0) {
  1192. int i;
  1193. for (i = 0; i < pkt.side_data_elems; i++)
  1194. av_free(pkt.side_data[i].data);
  1195. av_freep(&pkt.side_data);
  1196. pkt.side_data_elems = 0;
  1197. }
  1198. return ret ? ret : pkt.size;
  1199. }
  1200. #endif
  1201. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1202. AVPacket *avpkt,
  1203. const AVFrame *frame,
  1204. int *got_packet_ptr)
  1205. {
  1206. int ret;
  1207. AVPacket user_pkt = *avpkt;
  1208. int needs_realloc = !user_pkt.data;
  1209. *got_packet_ptr = 0;
  1210. if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1211. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1212. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1213. av_free_packet(avpkt);
  1214. av_init_packet(avpkt);
  1215. avpkt->size = 0;
  1216. return 0;
  1217. }
  1218. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1219. return AVERROR(EINVAL);
  1220. av_assert0(avctx->codec->encode2);
  1221. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1222. av_assert0(ret <= 0);
  1223. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1224. needs_realloc = 0;
  1225. if (user_pkt.data) {
  1226. if (user_pkt.size >= avpkt->size) {
  1227. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1228. } else {
  1229. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1230. avpkt->size = user_pkt.size;
  1231. ret = -1;
  1232. }
  1233. avpkt->data = user_pkt.data;
  1234. avpkt->destruct = user_pkt.destruct;
  1235. } else {
  1236. if (av_dup_packet(avpkt) < 0) {
  1237. ret = AVERROR(ENOMEM);
  1238. }
  1239. }
  1240. }
  1241. if (!ret) {
  1242. if (!*got_packet_ptr)
  1243. avpkt->size = 0;
  1244. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1245. avpkt->pts = avpkt->dts = frame->pts;
  1246. if (needs_realloc && avpkt->data &&
  1247. avpkt->destruct == av_destruct_packet) {
  1248. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1249. if (new_data)
  1250. avpkt->data = new_data;
  1251. }
  1252. avctx->frame_number++;
  1253. }
  1254. if (ret < 0 || !*got_packet_ptr)
  1255. av_free_packet(avpkt);
  1256. emms_c();
  1257. return ret;
  1258. }
  1259. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1260. const AVSubtitle *sub)
  1261. {
  1262. int ret;
  1263. if (sub->start_display_time) {
  1264. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1265. return -1;
  1266. }
  1267. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1268. avctx->frame_number++;
  1269. return ret;
  1270. }
  1271. /**
  1272. * Attempt to guess proper monotonic timestamps for decoded video frames
  1273. * which might have incorrect times. Input timestamps may wrap around, in
  1274. * which case the output will as well.
  1275. *
  1276. * @param pts the pts field of the decoded AVPacket, as passed through
  1277. * AVFrame.pkt_pts
  1278. * @param dts the dts field of the decoded AVPacket
  1279. * @return one of the input values, may be AV_NOPTS_VALUE
  1280. */
  1281. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1282. int64_t reordered_pts, int64_t dts)
  1283. {
  1284. int64_t pts = AV_NOPTS_VALUE;
  1285. if (dts != AV_NOPTS_VALUE) {
  1286. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1287. ctx->pts_correction_last_dts = dts;
  1288. }
  1289. if (reordered_pts != AV_NOPTS_VALUE) {
  1290. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1291. ctx->pts_correction_last_pts = reordered_pts;
  1292. }
  1293. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1294. && reordered_pts != AV_NOPTS_VALUE)
  1295. pts = reordered_pts;
  1296. else
  1297. pts = dts;
  1298. return pts;
  1299. }
  1300. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1301. {
  1302. int size = 0;
  1303. const uint8_t *data;
  1304. uint32_t flags;
  1305. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1306. return;
  1307. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1308. if (!data || size < 4)
  1309. return;
  1310. flags = bytestream_get_le32(&data);
  1311. size -= 4;
  1312. if (size < 4) /* Required for any of the changes */
  1313. return;
  1314. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1315. avctx->channels = bytestream_get_le32(&data);
  1316. size -= 4;
  1317. }
  1318. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1319. if (size < 8)
  1320. return;
  1321. avctx->channel_layout = bytestream_get_le64(&data);
  1322. size -= 8;
  1323. }
  1324. if (size < 4)
  1325. return;
  1326. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1327. avctx->sample_rate = bytestream_get_le32(&data);
  1328. size -= 4;
  1329. }
  1330. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1331. if (size < 8)
  1332. return;
  1333. avctx->width = bytestream_get_le32(&data);
  1334. avctx->height = bytestream_get_le32(&data);
  1335. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1336. size -= 8;
  1337. }
  1338. }
  1339. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1340. int *got_picture_ptr,
  1341. const AVPacket *avpkt)
  1342. {
  1343. int ret;
  1344. // copy to ensure we do not change avpkt
  1345. AVPacket tmp = *avpkt;
  1346. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1347. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1348. return AVERROR(EINVAL);
  1349. }
  1350. *got_picture_ptr = 0;
  1351. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1352. return AVERROR(EINVAL);
  1353. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1354. int did_split = av_packet_split_side_data(&tmp);
  1355. apply_param_change(avctx, &tmp);
  1356. avctx->pkt = &tmp;
  1357. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1358. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1359. &tmp);
  1360. else {
  1361. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1362. &tmp);
  1363. picture->pkt_dts = avpkt->dts;
  1364. if(!avctx->has_b_frames){
  1365. picture->pkt_pos = avpkt->pos;
  1366. }
  1367. //FIXME these should be under if(!avctx->has_b_frames)
  1368. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1369. if (!picture->width) picture->width = avctx->width;
  1370. if (!picture->height) picture->height = avctx->height;
  1371. if (picture->format == PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1372. }
  1373. emms_c(); //needed to avoid an emms_c() call before every return;
  1374. avctx->pkt = NULL;
  1375. if (did_split) {
  1376. ff_packet_free_side_data(&tmp);
  1377. if(ret == tmp.size)
  1378. ret = avpkt->size;
  1379. }
  1380. if (*got_picture_ptr){
  1381. avctx->frame_number++;
  1382. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1383. picture->pkt_pts,
  1384. picture->pkt_dts);
  1385. }
  1386. } else
  1387. ret = 0;
  1388. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1389. * make sure it's set correctly */
  1390. picture->extended_data = picture->data;
  1391. return ret;
  1392. }
  1393. #if FF_API_OLD_DECODE_AUDIO
  1394. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1395. int *frame_size_ptr,
  1396. AVPacket *avpkt)
  1397. {
  1398. AVFrame frame;
  1399. int ret, got_frame = 0;
  1400. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1401. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1402. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1403. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1404. "avcodec_decode_audio4()\n");
  1405. avctx->get_buffer = avcodec_default_get_buffer;
  1406. avctx->release_buffer = avcodec_default_release_buffer;
  1407. }
  1408. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1409. if (ret >= 0 && got_frame) {
  1410. int ch, plane_size;
  1411. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1412. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1413. frame.nb_samples,
  1414. avctx->sample_fmt, 1);
  1415. if (*frame_size_ptr < data_size) {
  1416. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1417. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1418. return AVERROR(EINVAL);
  1419. }
  1420. memcpy(samples, frame.extended_data[0], plane_size);
  1421. if (planar && avctx->channels > 1) {
  1422. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1423. for (ch = 1; ch < avctx->channels; ch++) {
  1424. memcpy(out, frame.extended_data[ch], plane_size);
  1425. out += plane_size;
  1426. }
  1427. }
  1428. *frame_size_ptr = data_size;
  1429. } else {
  1430. *frame_size_ptr = 0;
  1431. }
  1432. return ret;
  1433. }
  1434. #endif
  1435. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1436. AVFrame *frame,
  1437. int *got_frame_ptr,
  1438. const AVPacket *avpkt)
  1439. {
  1440. int planar, channels;
  1441. int ret = 0;
  1442. *got_frame_ptr = 0;
  1443. if (!avpkt->data && avpkt->size) {
  1444. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1445. return AVERROR(EINVAL);
  1446. }
  1447. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1448. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1449. return AVERROR(EINVAL);
  1450. }
  1451. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1452. uint8_t *side;
  1453. int side_size;
  1454. // copy to ensure we do not change avpkt
  1455. AVPacket tmp = *avpkt;
  1456. int did_split = av_packet_split_side_data(&tmp);
  1457. apply_param_change(avctx, &tmp);
  1458. avctx->pkt = &tmp;
  1459. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1460. if (ret >= 0 && *got_frame_ptr) {
  1461. avctx->frame_number++;
  1462. frame->pkt_dts = avpkt->dts;
  1463. frame->best_effort_timestamp = guess_correct_pts(avctx,
  1464. frame->pkt_pts,
  1465. frame->pkt_dts);
  1466. if (frame->format == AV_SAMPLE_FMT_NONE)
  1467. frame->format = avctx->sample_fmt;
  1468. if (!frame->channel_layout)
  1469. frame->channel_layout = avctx->channel_layout;
  1470. if (!frame->channels)
  1471. frame->channels = avctx->channels;
  1472. if (!frame->sample_rate)
  1473. frame->sample_rate = avctx->sample_rate;
  1474. }
  1475. side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1476. if(side && side_size>=10) {
  1477. avctx->internal->skip_samples = AV_RL32(side);
  1478. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1479. avctx->internal->skip_samples);
  1480. }
  1481. if (avctx->internal->skip_samples) {
  1482. if(frame->nb_samples <= avctx->internal->skip_samples){
  1483. *got_frame_ptr = 0;
  1484. avctx->internal->skip_samples -= frame->nb_samples;
  1485. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1486. avctx->internal->skip_samples);
  1487. } else {
  1488. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1489. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1490. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1491. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1492. (AVRational){1, avctx->sample_rate},
  1493. avctx->pkt_timebase);
  1494. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1495. frame->pkt_pts += diff_ts;
  1496. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1497. frame->pkt_dts += diff_ts;
  1498. if (frame->pkt_duration >= diff_ts)
  1499. frame->pkt_duration -= diff_ts;
  1500. } else {
  1501. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1502. }
  1503. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1504. avctx->internal->skip_samples, frame->nb_samples);
  1505. frame->nb_samples -= avctx->internal->skip_samples;
  1506. avctx->internal->skip_samples = 0;
  1507. }
  1508. }
  1509. avctx->pkt = NULL;
  1510. if (did_split) {
  1511. ff_packet_free_side_data(&tmp);
  1512. if(ret == tmp.size)
  1513. ret = avpkt->size;
  1514. }
  1515. }
  1516. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1517. * make sure it's set correctly; assume decoders that actually use
  1518. * extended_data are doing it correctly */
  1519. planar = av_sample_fmt_is_planar(frame->format);
  1520. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1521. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1522. frame->extended_data = frame->data;
  1523. return ret;
  1524. }
  1525. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1526. int *got_sub_ptr,
  1527. AVPacket *avpkt)
  1528. {
  1529. int ret;
  1530. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1531. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1532. return AVERROR(EINVAL);
  1533. }
  1534. avctx->pkt = avpkt;
  1535. *got_sub_ptr = 0;
  1536. avcodec_get_subtitle_defaults(sub);
  1537. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  1538. sub->pts = av_rescale_q(avpkt->pts,
  1539. avctx->pkt_timebase, AV_TIME_BASE_Q);
  1540. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1541. if (*got_sub_ptr)
  1542. avctx->frame_number++;
  1543. return ret;
  1544. }
  1545. void avsubtitle_free(AVSubtitle *sub)
  1546. {
  1547. int i;
  1548. for (i = 0; i < sub->num_rects; i++) {
  1549. av_freep(&sub->rects[i]->pict.data[0]);
  1550. av_freep(&sub->rects[i]->pict.data[1]);
  1551. av_freep(&sub->rects[i]->pict.data[2]);
  1552. av_freep(&sub->rects[i]->pict.data[3]);
  1553. av_freep(&sub->rects[i]->text);
  1554. av_freep(&sub->rects[i]->ass);
  1555. av_freep(&sub->rects[i]);
  1556. }
  1557. av_freep(&sub->rects);
  1558. memset(sub, 0, sizeof(AVSubtitle));
  1559. }
  1560. av_cold int avcodec_close(AVCodecContext *avctx)
  1561. {
  1562. /* If there is a user-supplied mutex locking routine, call it. */
  1563. if (ff_lockmgr_cb) {
  1564. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1565. return -1;
  1566. }
  1567. entangled_thread_counter++;
  1568. if (entangled_thread_counter != 1) {
  1569. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1570. entangled_thread_counter--;
  1571. return -1;
  1572. }
  1573. if (avcodec_is_open(avctx)) {
  1574. if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1575. entangled_thread_counter --;
  1576. ff_frame_thread_encoder_free(avctx);
  1577. entangled_thread_counter ++;
  1578. }
  1579. if (HAVE_THREADS && avctx->thread_opaque)
  1580. ff_thread_free(avctx);
  1581. if (avctx->codec && avctx->codec->close)
  1582. avctx->codec->close(avctx);
  1583. avcodec_default_free_buffers(avctx);
  1584. avctx->coded_frame = NULL;
  1585. avctx->internal->byte_buffer_size = 0;
  1586. av_freep(&avctx->internal->byte_buffer);
  1587. av_freep(&avctx->internal);
  1588. }
  1589. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1590. av_opt_free(avctx->priv_data);
  1591. av_opt_free(avctx);
  1592. av_freep(&avctx->priv_data);
  1593. if (av_codec_is_encoder(avctx->codec))
  1594. av_freep(&avctx->extradata);
  1595. avctx->codec = NULL;
  1596. avctx->active_thread_type = 0;
  1597. entangled_thread_counter--;
  1598. /* Release any user-supplied mutex. */
  1599. if (ff_lockmgr_cb) {
  1600. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1601. }
  1602. return 0;
  1603. }
  1604. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  1605. {
  1606. switch(id){
  1607. //This is for future deprecatec codec ids, its empty since
  1608. //last major bump but will fill up again over time, please don't remove it
  1609. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  1610. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  1611. default : return id;
  1612. }
  1613. }
  1614. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1615. {
  1616. AVCodec *p, *experimental = NULL;
  1617. p = first_avcodec;
  1618. id= remap_deprecated_codec_id(id);
  1619. while (p) {
  1620. if (av_codec_is_encoder(p) && p->id == id) {
  1621. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1622. experimental = p;
  1623. } else
  1624. return p;
  1625. }
  1626. p = p->next;
  1627. }
  1628. return experimental;
  1629. }
  1630. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1631. {
  1632. AVCodec *p;
  1633. if (!name)
  1634. return NULL;
  1635. p = first_avcodec;
  1636. while (p) {
  1637. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1638. return p;
  1639. p = p->next;
  1640. }
  1641. return NULL;
  1642. }
  1643. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1644. {
  1645. AVCodec *p, *experimental=NULL;
  1646. p = first_avcodec;
  1647. id= remap_deprecated_codec_id(id);
  1648. while (p) {
  1649. if (av_codec_is_decoder(p) && p->id == id) {
  1650. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1651. experimental = p;
  1652. } else
  1653. return p;
  1654. }
  1655. p = p->next;
  1656. }
  1657. return experimental;
  1658. }
  1659. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1660. {
  1661. AVCodec *p;
  1662. if (!name)
  1663. return NULL;
  1664. p = first_avcodec;
  1665. while (p) {
  1666. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1667. return p;
  1668. p = p->next;
  1669. }
  1670. return NULL;
  1671. }
  1672. const char *avcodec_get_name(enum AVCodecID id)
  1673. {
  1674. const AVCodecDescriptor *cd;
  1675. AVCodec *codec;
  1676. if (id == AV_CODEC_ID_NONE)
  1677. return "none";
  1678. cd = avcodec_descriptor_get(id);
  1679. if (cd)
  1680. return cd->name;
  1681. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1682. codec = avcodec_find_decoder(id);
  1683. if (codec)
  1684. return codec->name;
  1685. codec = avcodec_find_encoder(id);
  1686. if (codec)
  1687. return codec->name;
  1688. return "unknown_codec";
  1689. }
  1690. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1691. {
  1692. int i, len, ret = 0;
  1693. #define IS_PRINT(x) \
  1694. (((x) >= '0' && (x) <= '9') || \
  1695. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1696. ((x) == '.' || (x) == ' ' || (x) == '-'))
  1697. for (i = 0; i < 4; i++) {
  1698. len = snprintf(buf, buf_size,
  1699. IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1700. buf += len;
  1701. buf_size = buf_size > len ? buf_size - len : 0;
  1702. ret += len;
  1703. codec_tag >>= 8;
  1704. }
  1705. return ret;
  1706. }
  1707. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1708. {
  1709. const char *codec_type;
  1710. const char *codec_name;
  1711. const char *profile = NULL;
  1712. const AVCodec *p;
  1713. int bitrate;
  1714. AVRational display_aspect_ratio;
  1715. if (!buf || buf_size <= 0)
  1716. return;
  1717. codec_type = av_get_media_type_string(enc->codec_type);
  1718. codec_name = avcodec_get_name(enc->codec_id);
  1719. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1720. if (enc->codec)
  1721. p = enc->codec;
  1722. else
  1723. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1724. avcodec_find_decoder(enc->codec_id);
  1725. if (p)
  1726. profile = av_get_profile_name(p, enc->profile);
  1727. }
  1728. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1729. codec_name, enc->mb_decision ? " (hq)" : "");
  1730. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1731. if (profile)
  1732. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1733. if (enc->codec_tag) {
  1734. char tag_buf[32];
  1735. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1736. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1737. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1738. }
  1739. switch (enc->codec_type) {
  1740. case AVMEDIA_TYPE_VIDEO:
  1741. if (enc->pix_fmt != PIX_FMT_NONE) {
  1742. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1743. ", %s",
  1744. av_get_pix_fmt_name(enc->pix_fmt));
  1745. }
  1746. if (enc->width) {
  1747. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1748. ", %dx%d",
  1749. enc->width, enc->height);
  1750. if (enc->sample_aspect_ratio.num) {
  1751. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1752. enc->width * enc->sample_aspect_ratio.num,
  1753. enc->height * enc->sample_aspect_ratio.den,
  1754. 1024 * 1024);
  1755. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1756. " [SAR %d:%d DAR %d:%d]",
  1757. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1758. display_aspect_ratio.num, display_aspect_ratio.den);
  1759. }
  1760. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1761. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1762. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1763. ", %d/%d",
  1764. enc->time_base.num / g, enc->time_base.den / g);
  1765. }
  1766. }
  1767. if (encode) {
  1768. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1769. ", q=%d-%d", enc->qmin, enc->qmax);
  1770. }
  1771. break;
  1772. case AVMEDIA_TYPE_AUDIO:
  1773. if (enc->sample_rate) {
  1774. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1775. ", %d Hz", enc->sample_rate);
  1776. }
  1777. av_strlcat(buf, ", ", buf_size);
  1778. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1779. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1780. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1781. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1782. }
  1783. break;
  1784. default:
  1785. return;
  1786. }
  1787. if (encode) {
  1788. if (enc->flags & CODEC_FLAG_PASS1)
  1789. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1790. ", pass 1");
  1791. if (enc->flags & CODEC_FLAG_PASS2)
  1792. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1793. ", pass 2");
  1794. }
  1795. bitrate = get_bit_rate(enc);
  1796. if (bitrate != 0) {
  1797. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1798. ", %d kb/s", bitrate / 1000);
  1799. }
  1800. }
  1801. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1802. {
  1803. const AVProfile *p;
  1804. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1805. return NULL;
  1806. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1807. if (p->profile == profile)
  1808. return p->name;
  1809. return NULL;
  1810. }
  1811. unsigned avcodec_version(void)
  1812. {
  1813. // av_assert0(AV_CODEC_ID_V410==164);
  1814. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  1815. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  1816. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  1817. av_assert0(AV_CODEC_ID_SRT==94216);
  1818. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1819. return LIBAVCODEC_VERSION_INT;
  1820. }
  1821. const char *avcodec_configuration(void)
  1822. {
  1823. return FFMPEG_CONFIGURATION;
  1824. }
  1825. const char *avcodec_license(void)
  1826. {
  1827. #define LICENSE_PREFIX "libavcodec license: "
  1828. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1829. }
  1830. void avcodec_flush_buffers(AVCodecContext *avctx)
  1831. {
  1832. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1833. ff_thread_flush(avctx);
  1834. else if (avctx->codec->flush)
  1835. avctx->codec->flush(avctx);
  1836. avctx->pts_correction_last_pts =
  1837. avctx->pts_correction_last_dts = INT64_MIN;
  1838. }
  1839. static void video_free_buffers(AVCodecContext *s)
  1840. {
  1841. AVCodecInternal *avci = s->internal;
  1842. int i, j;
  1843. if (!avci->buffer)
  1844. return;
  1845. if (avci->buffer_count)
  1846. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1847. avci->buffer_count);
  1848. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  1849. InternalBuffer *buf = &avci->buffer[i];
  1850. for (j = 0; j < 4; j++) {
  1851. av_freep(&buf->base[j]);
  1852. buf->data[j] = NULL;
  1853. }
  1854. }
  1855. av_freep(&avci->buffer);
  1856. avci->buffer_count = 0;
  1857. }
  1858. static void audio_free_buffers(AVCodecContext *avctx)
  1859. {
  1860. AVCodecInternal *avci = avctx->internal;
  1861. InternalBuffer *buf;
  1862. if (!avci->buffer)
  1863. return;
  1864. buf = avci->buffer;
  1865. if (buf->extended_data) {
  1866. av_free(buf->extended_data[0]);
  1867. if (buf->extended_data != buf->data)
  1868. av_freep(&buf->extended_data);
  1869. }
  1870. av_freep(&avci->buffer);
  1871. }
  1872. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1873. {
  1874. switch (avctx->codec_type) {
  1875. case AVMEDIA_TYPE_VIDEO:
  1876. video_free_buffers(avctx);
  1877. break;
  1878. case AVMEDIA_TYPE_AUDIO:
  1879. audio_free_buffers(avctx);
  1880. break;
  1881. default:
  1882. break;
  1883. }
  1884. }
  1885. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1886. {
  1887. switch (codec_id) {
  1888. case AV_CODEC_ID_ADPCM_CT:
  1889. case AV_CODEC_ID_ADPCM_IMA_APC:
  1890. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1891. case AV_CODEC_ID_ADPCM_IMA_WS:
  1892. case AV_CODEC_ID_ADPCM_G722:
  1893. case AV_CODEC_ID_ADPCM_YAMAHA:
  1894. return 4;
  1895. case AV_CODEC_ID_PCM_ALAW:
  1896. case AV_CODEC_ID_PCM_MULAW:
  1897. case AV_CODEC_ID_PCM_S8:
  1898. case AV_CODEC_ID_PCM_U8:
  1899. case AV_CODEC_ID_PCM_ZORK:
  1900. return 8;
  1901. case AV_CODEC_ID_PCM_S16BE:
  1902. case AV_CODEC_ID_PCM_S16LE:
  1903. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1904. case AV_CODEC_ID_PCM_U16BE:
  1905. case AV_CODEC_ID_PCM_U16LE:
  1906. return 16;
  1907. case AV_CODEC_ID_PCM_S24DAUD:
  1908. case AV_CODEC_ID_PCM_S24BE:
  1909. case AV_CODEC_ID_PCM_S24LE:
  1910. case AV_CODEC_ID_PCM_U24BE:
  1911. case AV_CODEC_ID_PCM_U24LE:
  1912. return 24;
  1913. case AV_CODEC_ID_PCM_S32BE:
  1914. case AV_CODEC_ID_PCM_S32LE:
  1915. case AV_CODEC_ID_PCM_U32BE:
  1916. case AV_CODEC_ID_PCM_U32LE:
  1917. case AV_CODEC_ID_PCM_F32BE:
  1918. case AV_CODEC_ID_PCM_F32LE:
  1919. return 32;
  1920. case AV_CODEC_ID_PCM_F64BE:
  1921. case AV_CODEC_ID_PCM_F64LE:
  1922. return 64;
  1923. default:
  1924. return 0;
  1925. }
  1926. }
  1927. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1928. {
  1929. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  1930. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  1931. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  1932. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  1933. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  1934. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  1935. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  1936. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  1937. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  1938. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  1939. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  1940. };
  1941. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  1942. return AV_CODEC_ID_NONE;
  1943. if (be < 0 || be > 1)
  1944. be = AV_NE(1, 0);
  1945. return map[fmt][be];
  1946. }
  1947. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1948. {
  1949. switch (codec_id) {
  1950. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1951. return 2;
  1952. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1953. return 3;
  1954. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1955. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1956. case AV_CODEC_ID_ADPCM_IMA_QT:
  1957. case AV_CODEC_ID_ADPCM_SWF:
  1958. case AV_CODEC_ID_ADPCM_MS:
  1959. return 4;
  1960. default:
  1961. return av_get_exact_bits_per_sample(codec_id);
  1962. }
  1963. }
  1964. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1965. {
  1966. int id, sr, ch, ba, tag, bps;
  1967. id = avctx->codec_id;
  1968. sr = avctx->sample_rate;
  1969. ch = avctx->channels;
  1970. ba = avctx->block_align;
  1971. tag = avctx->codec_tag;
  1972. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1973. /* codecs with an exact constant bits per sample */
  1974. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  1975. return (frame_bytes * 8LL) / (bps * ch);
  1976. bps = avctx->bits_per_coded_sample;
  1977. /* codecs with a fixed packet duration */
  1978. switch (id) {
  1979. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1980. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1981. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1982. case AV_CODEC_ID_AMR_NB:
  1983. case AV_CODEC_ID_GSM:
  1984. case AV_CODEC_ID_QCELP:
  1985. case AV_CODEC_ID_RA_288: return 160;
  1986. case AV_CODEC_ID_IMC: return 256;
  1987. case AV_CODEC_ID_AMR_WB:
  1988. case AV_CODEC_ID_GSM_MS: return 320;
  1989. case AV_CODEC_ID_MP1: return 384;
  1990. case AV_CODEC_ID_ATRAC1: return 512;
  1991. case AV_CODEC_ID_ATRAC3: return 1024;
  1992. case AV_CODEC_ID_MP2:
  1993. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1994. case AV_CODEC_ID_AC3: return 1536;
  1995. }
  1996. if (sr > 0) {
  1997. /* calc from sample rate */
  1998. if (id == AV_CODEC_ID_TTA)
  1999. return 256 * sr / 245;
  2000. if (ch > 0) {
  2001. /* calc from sample rate and channels */
  2002. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2003. return (480 << (sr / 22050)) / ch;
  2004. }
  2005. }
  2006. if (ba > 0) {
  2007. /* calc from block_align */
  2008. if (id == AV_CODEC_ID_SIPR) {
  2009. switch (ba) {
  2010. case 20: return 160;
  2011. case 19: return 144;
  2012. case 29: return 288;
  2013. case 37: return 480;
  2014. }
  2015. } else if (id == AV_CODEC_ID_ILBC) {
  2016. switch (ba) {
  2017. case 38: return 160;
  2018. case 50: return 240;
  2019. }
  2020. }
  2021. }
  2022. if (frame_bytes > 0) {
  2023. /* calc from frame_bytes only */
  2024. if (id == AV_CODEC_ID_TRUESPEECH)
  2025. return 240 * (frame_bytes / 32);
  2026. if (id == AV_CODEC_ID_NELLYMOSER)
  2027. return 256 * (frame_bytes / 64);
  2028. if (id == AV_CODEC_ID_RA_144)
  2029. return 160 * (frame_bytes / 20);
  2030. if (bps > 0) {
  2031. /* calc from frame_bytes and bits_per_coded_sample */
  2032. if (id == AV_CODEC_ID_ADPCM_G726)
  2033. return frame_bytes * 8 / bps;
  2034. }
  2035. if (ch > 0) {
  2036. /* calc from frame_bytes and channels */
  2037. switch (id) {
  2038. case AV_CODEC_ID_ADPCM_4XM:
  2039. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2040. return (frame_bytes - 4 * ch) * 2 / ch;
  2041. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2042. return (frame_bytes - 4) * 2 / ch;
  2043. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2044. return (frame_bytes - 8) * 2 / ch;
  2045. case AV_CODEC_ID_ADPCM_XA:
  2046. return (frame_bytes / 128) * 224 / ch;
  2047. case AV_CODEC_ID_INTERPLAY_DPCM:
  2048. return (frame_bytes - 6 - ch) / ch;
  2049. case AV_CODEC_ID_ROQ_DPCM:
  2050. return (frame_bytes - 8) / ch;
  2051. case AV_CODEC_ID_XAN_DPCM:
  2052. return (frame_bytes - 2 * ch) / ch;
  2053. case AV_CODEC_ID_MACE3:
  2054. return 3 * frame_bytes / ch;
  2055. case AV_CODEC_ID_MACE6:
  2056. return 6 * frame_bytes / ch;
  2057. case AV_CODEC_ID_PCM_LXF:
  2058. return 2 * (frame_bytes / (5 * ch));
  2059. }
  2060. if (tag) {
  2061. /* calc from frame_bytes, channels, and codec_tag */
  2062. if (id == AV_CODEC_ID_SOL_DPCM) {
  2063. if (tag == 3)
  2064. return frame_bytes / ch;
  2065. else
  2066. return frame_bytes * 2 / ch;
  2067. }
  2068. }
  2069. if (ba > 0) {
  2070. /* calc from frame_bytes, channels, and block_align */
  2071. int blocks = frame_bytes / ba;
  2072. switch (avctx->codec_id) {
  2073. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2074. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2075. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2076. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2077. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2078. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2079. case AV_CODEC_ID_ADPCM_MS:
  2080. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2081. }
  2082. }
  2083. if (bps > 0) {
  2084. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2085. switch (avctx->codec_id) {
  2086. case AV_CODEC_ID_PCM_DVD:
  2087. if(bps<4)
  2088. return 0;
  2089. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2090. case AV_CODEC_ID_PCM_BLURAY:
  2091. if(bps<4)
  2092. return 0;
  2093. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2094. case AV_CODEC_ID_S302M:
  2095. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2096. }
  2097. }
  2098. }
  2099. }
  2100. return 0;
  2101. }
  2102. #if !HAVE_THREADS
  2103. int ff_thread_init(AVCodecContext *s)
  2104. {
  2105. return -1;
  2106. }
  2107. #endif
  2108. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2109. {
  2110. unsigned int n = 0;
  2111. while (v >= 0xff) {
  2112. *s++ = 0xff;
  2113. v -= 0xff;
  2114. n++;
  2115. }
  2116. *s = v;
  2117. n++;
  2118. return n;
  2119. }
  2120. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2121. {
  2122. int i;
  2123. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2124. return i;
  2125. }
  2126. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2127. {
  2128. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  2129. "version to the newest one from Git. If the problem still "
  2130. "occurs, it means that your file has a feature which has not "
  2131. "been implemented.\n", feature);
  2132. if(want_sample)
  2133. av_log_ask_for_sample(avc, NULL);
  2134. }
  2135. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2136. {
  2137. va_list argument_list;
  2138. va_start(argument_list, msg);
  2139. if (msg)
  2140. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2141. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2142. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2143. "and contact the ffmpeg-devel mailing list.\n");
  2144. va_end(argument_list);
  2145. }
  2146. static AVHWAccel *first_hwaccel = NULL;
  2147. void av_register_hwaccel(AVHWAccel *hwaccel)
  2148. {
  2149. AVHWAccel **p = &first_hwaccel;
  2150. while (*p)
  2151. p = &(*p)->next;
  2152. *p = hwaccel;
  2153. hwaccel->next = NULL;
  2154. }
  2155. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2156. {
  2157. return hwaccel ? hwaccel->next : first_hwaccel;
  2158. }
  2159. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum PixelFormat pix_fmt)
  2160. {
  2161. AVHWAccel *hwaccel = NULL;
  2162. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2163. if (hwaccel->id == codec_id
  2164. && hwaccel->pix_fmt == pix_fmt)
  2165. return hwaccel;
  2166. return NULL;
  2167. }
  2168. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2169. {
  2170. if (ff_lockmgr_cb) {
  2171. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2172. return -1;
  2173. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2174. return -1;
  2175. }
  2176. ff_lockmgr_cb = cb;
  2177. if (ff_lockmgr_cb) {
  2178. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2179. return -1;
  2180. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2181. return -1;
  2182. }
  2183. return 0;
  2184. }
  2185. int avpriv_lock_avformat(void)
  2186. {
  2187. if (ff_lockmgr_cb) {
  2188. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2189. return -1;
  2190. }
  2191. return 0;
  2192. }
  2193. int avpriv_unlock_avformat(void)
  2194. {
  2195. if (ff_lockmgr_cb) {
  2196. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2197. return -1;
  2198. }
  2199. return 0;
  2200. }
  2201. unsigned int avpriv_toupper4(unsigned int x)
  2202. {
  2203. return toupper(x & 0xFF)
  2204. + (toupper((x >> 8) & 0xFF) << 8)
  2205. + (toupper((x >> 16) & 0xFF) << 16)
  2206. + (toupper((x >> 24) & 0xFF) << 24);
  2207. }
  2208. #if !HAVE_THREADS
  2209. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2210. {
  2211. f->owner = avctx;
  2212. ff_init_buffer_info(avctx, f);
  2213. return avctx->get_buffer(avctx, f);
  2214. }
  2215. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2216. {
  2217. f->owner->release_buffer(f->owner, f);
  2218. }
  2219. void ff_thread_finish_setup(AVCodecContext *avctx)
  2220. {
  2221. }
  2222. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2223. {
  2224. }
  2225. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2226. {
  2227. }
  2228. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2229. {
  2230. return 1;
  2231. }
  2232. #endif
  2233. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2234. {
  2235. AVCodec *c= avcodec_find_decoder(codec_id);
  2236. if(!c)
  2237. c= avcodec_find_encoder(codec_id);
  2238. if(c)
  2239. return c->type;
  2240. if (codec_id <= AV_CODEC_ID_NONE)
  2241. return AVMEDIA_TYPE_UNKNOWN;
  2242. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2243. return AVMEDIA_TYPE_VIDEO;
  2244. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2245. return AVMEDIA_TYPE_AUDIO;
  2246. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2247. return AVMEDIA_TYPE_SUBTITLE;
  2248. return AVMEDIA_TYPE_UNKNOWN;
  2249. }
  2250. int avcodec_is_open(AVCodecContext *s)
  2251. {
  2252. return !!s->internal;
  2253. }