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.

2184 lines
66KB

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