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.

2536 lines
81KB

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