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.

2095 lines
63KB

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