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.

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