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.

2133 lines
64KB

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