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.

1550 lines
46KB

  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/avstring.h"
  27. #include "libavutil/crc.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/audioconvert.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "libavutil/dict.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #include "libavutil/opt.h"
  37. #include "imgconvert.h"
  38. #include "thread.h"
  39. #include "audioconvert.h"
  40. #include "internal.h"
  41. #include <stdlib.h>
  42. #include <stdarg.h>
  43. #include <limits.h>
  44. #include <float.h>
  45. static int volatile entangled_thread_counter=0;
  46. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  47. static void *codec_mutex;
  48. static void *avformat_mutex;
  49. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  50. {
  51. if(min_size < *size)
  52. return ptr;
  53. min_size= FFMAX(17*min_size/16 + 32, min_size);
  54. ptr= av_realloc(ptr, min_size);
  55. 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
  56. min_size= 0;
  57. *size= min_size;
  58. return ptr;
  59. }
  60. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  61. {
  62. void **p = ptr;
  63. if (min_size < *size)
  64. return;
  65. min_size= FFMAX(17*min_size/16 + 32, min_size);
  66. av_free(*p);
  67. *p = av_malloc(min_size);
  68. if (!*p) min_size = 0;
  69. *size= min_size;
  70. }
  71. /* encoder management */
  72. static AVCodec *first_avcodec = NULL;
  73. AVCodec *av_codec_next(AVCodec *c){
  74. if(c) return c->next;
  75. else return first_avcodec;
  76. }
  77. #if !FF_API_AVCODEC_INIT
  78. static
  79. #endif
  80. void avcodec_init(void)
  81. {
  82. static int initialized = 0;
  83. if (initialized != 0)
  84. return;
  85. initialized = 1;
  86. dsputil_static_init();
  87. }
  88. void avcodec_register(AVCodec *codec)
  89. {
  90. AVCodec **p;
  91. avcodec_init();
  92. p = &first_avcodec;
  93. while (*p != NULL) p = &(*p)->next;
  94. *p = codec;
  95. codec->next = NULL;
  96. if (codec->init_static_data)
  97. codec->init_static_data(codec);
  98. }
  99. unsigned avcodec_get_edge_width(void)
  100. {
  101. return EDGE_WIDTH;
  102. }
  103. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  104. s->coded_width = width;
  105. s->coded_height= height;
  106. s->width = -((-width )>>s->lowres);
  107. s->height= -((-height)>>s->lowres);
  108. }
  109. #define INTERNAL_BUFFER_SIZE (32+1)
  110. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  111. int linesize_align[AV_NUM_DATA_POINTERS])
  112. {
  113. int i;
  114. int w_align= 1;
  115. int h_align= 1;
  116. switch(s->pix_fmt){
  117. case PIX_FMT_YUV420P:
  118. case PIX_FMT_YUYV422:
  119. case PIX_FMT_UYVY422:
  120. case PIX_FMT_YUV422P:
  121. case PIX_FMT_YUV440P:
  122. case PIX_FMT_YUV444P:
  123. case PIX_FMT_GBRP:
  124. case PIX_FMT_GRAY8:
  125. case PIX_FMT_GRAY16BE:
  126. case PIX_FMT_GRAY16LE:
  127. case PIX_FMT_YUVJ420P:
  128. case PIX_FMT_YUVJ422P:
  129. case PIX_FMT_YUVJ440P:
  130. case PIX_FMT_YUVJ444P:
  131. case PIX_FMT_YUVA420P:
  132. case PIX_FMT_YUV420P9LE:
  133. case PIX_FMT_YUV420P9BE:
  134. case PIX_FMT_YUV420P10LE:
  135. case PIX_FMT_YUV420P10BE:
  136. case PIX_FMT_YUV422P9LE:
  137. case PIX_FMT_YUV422P9BE:
  138. case PIX_FMT_YUV422P10LE:
  139. case PIX_FMT_YUV422P10BE:
  140. case PIX_FMT_YUV444P9LE:
  141. case PIX_FMT_YUV444P9BE:
  142. case PIX_FMT_YUV444P10LE:
  143. case PIX_FMT_YUV444P10BE:
  144. case PIX_FMT_GBRP9LE:
  145. case PIX_FMT_GBRP9BE:
  146. case PIX_FMT_GBRP10LE:
  147. case PIX_FMT_GBRP10BE:
  148. w_align = 16; //FIXME assume 16 pixel per macroblock
  149. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  150. break;
  151. case PIX_FMT_YUV411P:
  152. case PIX_FMT_UYYVYY411:
  153. w_align=32;
  154. h_align=8;
  155. break;
  156. case PIX_FMT_YUV410P:
  157. if(s->codec_id == CODEC_ID_SVQ1){
  158. w_align=64;
  159. h_align=64;
  160. }
  161. case PIX_FMT_RGB555:
  162. if(s->codec_id == CODEC_ID_RPZA){
  163. w_align=4;
  164. h_align=4;
  165. }
  166. case PIX_FMT_PAL8:
  167. case PIX_FMT_BGR8:
  168. case PIX_FMT_RGB8:
  169. if(s->codec_id == CODEC_ID_SMC){
  170. w_align=4;
  171. h_align=4;
  172. }
  173. break;
  174. case PIX_FMT_BGR24:
  175. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  176. w_align=4;
  177. h_align=4;
  178. }
  179. break;
  180. default:
  181. w_align= 1;
  182. h_align= 1;
  183. break;
  184. }
  185. *width = FFALIGN(*width , w_align);
  186. *height= FFALIGN(*height, h_align);
  187. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  188. *height+=2; // some of the optimized chroma MC reads one line too much
  189. // which is also done in mpeg decoders with lowres > 0
  190. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  191. linesize_align[i] = STRIDE_ALIGN;
  192. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  193. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  194. //picture size unneccessarily in some cases. The solution here is not
  195. //pretty and better ideas are welcome!
  196. #if HAVE_MMX
  197. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  198. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  199. s->codec_id == CODEC_ID_VP6A) {
  200. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  201. linesize_align[i] = 16;
  202. }
  203. #endif
  204. }
  205. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  206. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  207. int linesize_align[AV_NUM_DATA_POINTERS];
  208. int align;
  209. avcodec_align_dimensions2(s, width, height, linesize_align);
  210. align = FFMAX(linesize_align[0], linesize_align[3]);
  211. linesize_align[1] <<= chroma_shift;
  212. linesize_align[2] <<= chroma_shift;
  213. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  214. *width=FFALIGN(*width, align);
  215. }
  216. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  217. {
  218. AVCodecInternal *avci = avctx->internal;
  219. InternalBuffer *buf;
  220. int buf_size, ret, i, needs_extended_data;
  221. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  222. frame->nb_samples, avctx->sample_fmt,
  223. 32);
  224. if (buf_size < 0)
  225. return AVERROR(EINVAL);
  226. needs_extended_data = av_sample_fmt_is_planar(avctx->sample_fmt) &&
  227. avctx->channels > AV_NUM_DATA_POINTERS;
  228. /* allocate InternalBuffer if needed */
  229. if (!avci->buffer) {
  230. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  231. if (!avci->buffer)
  232. return AVERROR(ENOMEM);
  233. }
  234. buf = avci->buffer;
  235. /* if there is a previously-used internal buffer, check its size and
  236. channel count to see if we can reuse it */
  237. if (buf->extended_data) {
  238. /* if current buffer is too small, free it */
  239. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  240. av_free(buf->extended_data[0]);
  241. if (buf->extended_data != buf->data)
  242. av_free(&buf->extended_data);
  243. buf->extended_data = NULL;
  244. buf->data[0] = NULL;
  245. }
  246. /* if number of channels has changed, reset and/or free extended data
  247. pointers but leave data buffer in buf->data[0] for reuse */
  248. if (buf->nb_channels != avctx->channels) {
  249. if (buf->extended_data != buf->data)
  250. av_free(buf->extended_data);
  251. buf->extended_data = NULL;
  252. }
  253. }
  254. /* if there is no previous buffer or the previous buffer cannot be used
  255. as-is, allocate a new buffer and/or rearrange the channel pointers */
  256. if (!buf->extended_data) {
  257. /* if the channel pointers will fit, just set extended_data to data,
  258. otherwise allocate the extended_data channel pointers */
  259. if (needs_extended_data) {
  260. buf->extended_data = av_mallocz(avctx->channels *
  261. sizeof(*buf->extended_data));
  262. if (!buf->extended_data)
  263. return AVERROR(ENOMEM);
  264. } else {
  265. buf->extended_data = buf->data;
  266. }
  267. /* if there is a previous buffer and it is large enough, reuse it and
  268. just fill-in new channel pointers and linesize, otherwise allocate
  269. a new buffer */
  270. if (buf->extended_data[0]) {
  271. ret = av_samples_fill_arrays(buf->extended_data, &buf->linesize[0],
  272. buf->extended_data[0], avctx->channels,
  273. frame->nb_samples, avctx->sample_fmt,
  274. 32);
  275. } else {
  276. ret = av_samples_alloc(buf->extended_data, &buf->linesize[0],
  277. avctx->channels, frame->nb_samples,
  278. avctx->sample_fmt, 32);
  279. }
  280. if (ret)
  281. return ret;
  282. /* if data was not used for extended_data, we need to copy as many of
  283. the extended_data channel pointers as will fit */
  284. if (needs_extended_data) {
  285. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  286. buf->data[i] = buf->extended_data[i];
  287. }
  288. buf->audio_data_size = buf_size;
  289. buf->nb_channels = avctx->channels;
  290. }
  291. /* copy InternalBuffer info to the AVFrame */
  292. frame->type = FF_BUFFER_TYPE_INTERNAL;
  293. frame->extended_data = buf->extended_data;
  294. frame->linesize[0] = buf->linesize[0];
  295. memcpy(frame->data, buf->data, sizeof(frame->data));
  296. if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
  297. else frame->pkt_pts = AV_NOPTS_VALUE;
  298. frame->reordered_opaque = avctx->reordered_opaque;
  299. if (avctx->debug & FF_DEBUG_BUFFERS)
  300. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  301. "internal audio buffer used\n", frame);
  302. return 0;
  303. }
  304. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  305. {
  306. int i;
  307. int w= s->width;
  308. int h= s->height;
  309. InternalBuffer *buf;
  310. AVCodecInternal *avci = s->internal;
  311. if(pic->data[0]!=NULL) {
  312. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  313. return -1;
  314. }
  315. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  316. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  317. return -1;
  318. }
  319. if(av_image_check_size(w, h, 0, s))
  320. return -1;
  321. if (!avci->buffer) {
  322. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  323. sizeof(InternalBuffer));
  324. }
  325. buf = &avci->buffer[avci->buffer_count];
  326. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  327. if(s->active_thread_type&FF_THREAD_FRAME) {
  328. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  329. return -1;
  330. }
  331. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  332. av_freep(&buf->base[i]);
  333. buf->data[i]= NULL;
  334. }
  335. }
  336. if (!buf->base[0]) {
  337. int h_chroma_shift, v_chroma_shift;
  338. int size[4] = {0};
  339. int tmpsize;
  340. int unaligned;
  341. AVPicture picture;
  342. int stride_align[AV_NUM_DATA_POINTERS];
  343. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  344. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  345. avcodec_align_dimensions2(s, &w, &h, stride_align);
  346. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  347. w+= EDGE_WIDTH*2;
  348. h+= EDGE_WIDTH*2;
  349. }
  350. do {
  351. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  352. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  353. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  354. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  355. w += w & ~(w-1);
  356. unaligned = 0;
  357. for (i=0; i<4; i++){
  358. unaligned |= picture.linesize[i] % stride_align[i];
  359. }
  360. } while (unaligned);
  361. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  362. if (tmpsize < 0)
  363. return -1;
  364. for (i=0; i<3 && picture.data[i+1]; i++)
  365. size[i] = picture.data[i+1] - picture.data[i];
  366. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  367. memset(buf->base, 0, sizeof(buf->base));
  368. memset(buf->data, 0, sizeof(buf->data));
  369. for(i=0; i<4 && size[i]; i++){
  370. const int h_shift= i==0 ? 0 : h_chroma_shift;
  371. const int v_shift= i==0 ? 0 : v_chroma_shift;
  372. buf->linesize[i]= picture.linesize[i];
  373. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  374. if(buf->base[i]==NULL) return -1;
  375. memset(buf->base[i], 128, size[i]);
  376. // no edge if EDGE EMU or not planar YUV
  377. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  378. buf->data[i] = buf->base[i];
  379. else
  380. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  381. }
  382. for (; i < AV_NUM_DATA_POINTERS; i++) {
  383. buf->base[i] = buf->data[i] = NULL;
  384. buf->linesize[i] = 0;
  385. }
  386. if(size[1] && !size[2])
  387. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  388. buf->width = s->width;
  389. buf->height = s->height;
  390. buf->pix_fmt= s->pix_fmt;
  391. }
  392. pic->type= FF_BUFFER_TYPE_INTERNAL;
  393. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  394. pic->base[i]= buf->base[i];
  395. pic->data[i]= buf->data[i];
  396. pic->linesize[i]= buf->linesize[i];
  397. }
  398. pic->extended_data = pic->data;
  399. avci->buffer_count++;
  400. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  401. else pic->pkt_pts= AV_NOPTS_VALUE;
  402. pic->reordered_opaque= s->reordered_opaque;
  403. if(s->debug&FF_DEBUG_BUFFERS)
  404. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  405. "buffers used\n", pic, avci->buffer_count);
  406. return 0;
  407. }
  408. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  409. {
  410. switch (avctx->codec_type) {
  411. case AVMEDIA_TYPE_VIDEO:
  412. return video_get_buffer(avctx, frame);
  413. case AVMEDIA_TYPE_AUDIO:
  414. return audio_get_buffer(avctx, frame);
  415. default:
  416. return -1;
  417. }
  418. }
  419. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  420. int i;
  421. InternalBuffer *buf, *last;
  422. AVCodecInternal *avci = s->internal;
  423. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  424. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  425. assert(avci->buffer_count);
  426. if (avci->buffer) {
  427. buf = NULL; /* avoids warning */
  428. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  429. buf = &avci->buffer[i];
  430. if (buf->data[0] == pic->data[0])
  431. break;
  432. }
  433. assert(i < avci->buffer_count);
  434. avci->buffer_count--;
  435. last = &avci->buffer[avci->buffer_count];
  436. if (buf != last)
  437. FFSWAP(InternalBuffer, *buf, *last);
  438. }
  439. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  440. pic->data[i]=NULL;
  441. // pic->base[i]=NULL;
  442. }
  443. //printf("R%X\n", pic->opaque);
  444. if(s->debug&FF_DEBUG_BUFFERS)
  445. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  446. "buffers used\n", pic, avci->buffer_count);
  447. }
  448. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  449. AVFrame temp_pic;
  450. int i;
  451. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  452. /* If no picture return a new buffer */
  453. if(pic->data[0] == NULL) {
  454. /* We will copy from buffer, so must be readable */
  455. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  456. return s->get_buffer(s, pic);
  457. }
  458. /* If internal buffer type return the same buffer */
  459. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  460. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  461. else pic->pkt_pts= AV_NOPTS_VALUE;
  462. pic->reordered_opaque= s->reordered_opaque;
  463. return 0;
  464. }
  465. /*
  466. * Not internal type and reget_buffer not overridden, emulate cr buffer
  467. */
  468. temp_pic = *pic;
  469. for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
  470. pic->data[i] = pic->base[i] = NULL;
  471. pic->opaque = NULL;
  472. /* Allocate new frame */
  473. if (s->get_buffer(s, pic))
  474. return -1;
  475. /* Copy image data from old buffer to new buffer */
  476. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  477. s->height);
  478. s->release_buffer(s, &temp_pic); // Release old frame
  479. return 0;
  480. }
  481. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  482. int i;
  483. for(i=0; i<count; i++){
  484. int r= func(c, (char*)arg + i*size);
  485. if(ret) ret[i]= r;
  486. }
  487. return 0;
  488. }
  489. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  490. int i;
  491. for(i=0; i<count; i++){
  492. int r= func(c, arg, i, 0);
  493. if(ret) ret[i]= r;
  494. }
  495. return 0;
  496. }
  497. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  498. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  499. ++fmt;
  500. return fmt[0];
  501. }
  502. void avcodec_get_frame_defaults(AVFrame *pic){
  503. memset(pic, 0, sizeof(AVFrame));
  504. pic->pts= AV_NOPTS_VALUE;
  505. pic->key_frame= 1;
  506. }
  507. AVFrame *avcodec_alloc_frame(void){
  508. AVFrame *pic= av_malloc(sizeof(AVFrame));
  509. if(pic==NULL) return NULL;
  510. avcodec_get_frame_defaults(pic);
  511. return pic;
  512. }
  513. #if FF_API_AVCODEC_OPEN
  514. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  515. {
  516. return avcodec_open2(avctx, codec, NULL);
  517. }
  518. #endif
  519. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  520. {
  521. int ret = 0;
  522. AVDictionary *tmp = NULL;
  523. if (options)
  524. av_dict_copy(&tmp, *options, 0);
  525. /* If there is a user-supplied mutex locking routine, call it. */
  526. if (ff_lockmgr_cb) {
  527. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  528. return -1;
  529. }
  530. entangled_thread_counter++;
  531. if(entangled_thread_counter != 1){
  532. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  533. ret = -1;
  534. goto end;
  535. }
  536. if(avctx->codec || !codec) {
  537. ret = AVERROR(EINVAL);
  538. goto end;
  539. }
  540. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  541. if (!avctx->internal) {
  542. ret = AVERROR(ENOMEM);
  543. goto end;
  544. }
  545. if (codec->priv_data_size > 0) {
  546. if(!avctx->priv_data){
  547. avctx->priv_data = av_mallocz(codec->priv_data_size);
  548. if (!avctx->priv_data) {
  549. ret = AVERROR(ENOMEM);
  550. goto end;
  551. }
  552. if (codec->priv_class) {
  553. *(AVClass**)avctx->priv_data= codec->priv_class;
  554. av_opt_set_defaults(avctx->priv_data);
  555. }
  556. }
  557. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  558. goto free_and_end;
  559. } else {
  560. avctx->priv_data = NULL;
  561. }
  562. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  563. goto free_and_end;
  564. if(avctx->coded_width && avctx->coded_height)
  565. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  566. else if(avctx->width && avctx->height)
  567. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  568. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  569. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  570. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  571. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  572. avcodec_set_dimensions(avctx, 0, 0);
  573. }
  574. /* if the decoder init function was already called previously,
  575. free the already allocated subtitle_header before overwriting it */
  576. if (codec->decode)
  577. av_freep(&avctx->subtitle_header);
  578. #define SANE_NB_CHANNELS 128U
  579. if (avctx->channels > SANE_NB_CHANNELS) {
  580. ret = AVERROR(EINVAL);
  581. goto free_and_end;
  582. }
  583. avctx->codec = codec;
  584. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  585. avctx->codec_id == CODEC_ID_NONE) {
  586. avctx->codec_type = codec->type;
  587. avctx->codec_id = codec->id;
  588. }
  589. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  590. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  591. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  592. ret = AVERROR(EINVAL);
  593. goto free_and_end;
  594. }
  595. avctx->frame_number = 0;
  596. #if FF_API_ER
  597. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
  598. avctx->error_recognition, avctx->err_recognition);
  599. /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
  600. FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
  601. avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
  602. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
  603. avctx->error_recognition, avctx->err_recognition);
  604. #endif
  605. if (HAVE_THREADS && !avctx->thread_opaque) {
  606. ret = ff_thread_init(avctx);
  607. if (ret < 0) {
  608. goto free_and_end;
  609. }
  610. }
  611. if (avctx->codec->max_lowres < avctx->lowres) {
  612. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  613. avctx->codec->max_lowres);
  614. ret = AVERROR(EINVAL);
  615. goto free_and_end;
  616. }
  617. if (avctx->codec->encode) {
  618. int i;
  619. if (avctx->codec->sample_fmts) {
  620. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  621. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  622. break;
  623. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  624. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  625. ret = AVERROR(EINVAL);
  626. goto free_and_end;
  627. }
  628. }
  629. if (avctx->codec->supported_samplerates) {
  630. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  631. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  632. break;
  633. if (avctx->codec->supported_samplerates[i] == 0) {
  634. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  635. ret = AVERROR(EINVAL);
  636. goto free_and_end;
  637. }
  638. }
  639. if (avctx->codec->channel_layouts) {
  640. if (!avctx->channel_layout) {
  641. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  642. } else {
  643. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  644. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  645. break;
  646. if (avctx->codec->channel_layouts[i] == 0) {
  647. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  648. ret = AVERROR(EINVAL);
  649. goto free_and_end;
  650. }
  651. }
  652. }
  653. if (avctx->channel_layout && avctx->channels) {
  654. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  655. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  656. ret = AVERROR(EINVAL);
  657. goto free_and_end;
  658. }
  659. } else if (avctx->channel_layout) {
  660. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  661. }
  662. }
  663. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  664. ret = avctx->codec->init(avctx);
  665. if (ret < 0) {
  666. goto free_and_end;
  667. }
  668. }
  669. end:
  670. entangled_thread_counter--;
  671. /* Release any user-supplied mutex. */
  672. if (ff_lockmgr_cb) {
  673. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  674. }
  675. if (options) {
  676. av_dict_free(options);
  677. *options = tmp;
  678. }
  679. return ret;
  680. free_and_end:
  681. av_dict_free(&tmp);
  682. av_freep(&avctx->priv_data);
  683. av_freep(&avctx->internal);
  684. avctx->codec= NULL;
  685. goto end;
  686. }
  687. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  688. const short *samples)
  689. {
  690. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  691. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  692. return -1;
  693. }
  694. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  695. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  696. avctx->frame_number++;
  697. return ret;
  698. }else
  699. return 0;
  700. }
  701. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  702. const AVFrame *pict)
  703. {
  704. if(buf_size < FF_MIN_BUFFER_SIZE){
  705. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  706. return -1;
  707. }
  708. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  709. return -1;
  710. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  711. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  712. avctx->frame_number++;
  713. emms_c(); //needed to avoid an emms_c() call before every return;
  714. return ret;
  715. }else
  716. return 0;
  717. }
  718. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  719. const AVSubtitle *sub)
  720. {
  721. int ret;
  722. if(sub->start_display_time) {
  723. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  724. return -1;
  725. }
  726. if(sub->num_rects == 0 || !sub->rects)
  727. return -1;
  728. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  729. avctx->frame_number++;
  730. return ret;
  731. }
  732. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  733. int *got_picture_ptr,
  734. AVPacket *avpkt)
  735. {
  736. int ret;
  737. *got_picture_ptr= 0;
  738. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  739. return -1;
  740. avctx->pkt = avpkt;
  741. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  742. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  743. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  744. avpkt);
  745. else {
  746. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  747. avpkt);
  748. picture->pkt_dts= avpkt->dts;
  749. }
  750. emms_c(); //needed to avoid an emms_c() call before every return;
  751. if (*got_picture_ptr)
  752. avctx->frame_number++;
  753. }else
  754. ret= 0;
  755. return ret;
  756. }
  757. #if FF_API_OLD_DECODE_AUDIO
  758. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  759. int *frame_size_ptr,
  760. AVPacket *avpkt)
  761. {
  762. AVFrame frame;
  763. int ret, got_frame = 0;
  764. if (avctx->get_buffer != avcodec_default_get_buffer) {
  765. av_log(avctx, AV_LOG_ERROR, "A custom get_buffer() cannot be used with "
  766. "avcodec_decode_audio3()\n");
  767. return AVERROR(EINVAL);
  768. }
  769. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  770. if (ret >= 0 && got_frame) {
  771. int ch, plane_size;
  772. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  773. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  774. frame.nb_samples,
  775. avctx->sample_fmt, 1);
  776. if (*frame_size_ptr < data_size) {
  777. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  778. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  779. return AVERROR(EINVAL);
  780. }
  781. memcpy(samples, frame.extended_data[0], plane_size);
  782. if (planar && avctx->channels > 1) {
  783. uint8_t *out = ((uint8_t *)samples) + plane_size;
  784. for (ch = 1; ch < avctx->channels; ch++) {
  785. memcpy(out, frame.extended_data[ch], plane_size);
  786. out += plane_size;
  787. }
  788. }
  789. *frame_size_ptr = data_size;
  790. } else {
  791. *frame_size_ptr = 0;
  792. }
  793. return ret;
  794. }
  795. #endif
  796. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  797. AVFrame *frame,
  798. int *got_frame_ptr,
  799. AVPacket *avpkt)
  800. {
  801. int ret = 0;
  802. *got_frame_ptr = 0;
  803. avctx->pkt = avpkt;
  804. if (!avpkt->data && avpkt->size) {
  805. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  806. return AVERROR(EINVAL);
  807. }
  808. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  809. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  810. if (ret >= 0 && *got_frame_ptr) {
  811. avctx->frame_number++;
  812. frame->pkt_dts = avpkt->dts;
  813. }
  814. }
  815. return ret;
  816. }
  817. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  818. int *got_sub_ptr,
  819. AVPacket *avpkt)
  820. {
  821. int ret;
  822. avctx->pkt = avpkt;
  823. *got_sub_ptr = 0;
  824. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  825. if (*got_sub_ptr)
  826. avctx->frame_number++;
  827. return ret;
  828. }
  829. void avsubtitle_free(AVSubtitle *sub)
  830. {
  831. int i;
  832. for (i = 0; i < sub->num_rects; i++)
  833. {
  834. av_freep(&sub->rects[i]->pict.data[0]);
  835. av_freep(&sub->rects[i]->pict.data[1]);
  836. av_freep(&sub->rects[i]->pict.data[2]);
  837. av_freep(&sub->rects[i]->pict.data[3]);
  838. av_freep(&sub->rects[i]->text);
  839. av_freep(&sub->rects[i]->ass);
  840. av_freep(&sub->rects[i]);
  841. }
  842. av_freep(&sub->rects);
  843. memset(sub, 0, sizeof(AVSubtitle));
  844. }
  845. av_cold int avcodec_close(AVCodecContext *avctx)
  846. {
  847. /* If there is a user-supplied mutex locking routine, call it. */
  848. if (ff_lockmgr_cb) {
  849. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  850. return -1;
  851. }
  852. entangled_thread_counter++;
  853. if(entangled_thread_counter != 1){
  854. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  855. entangled_thread_counter--;
  856. return -1;
  857. }
  858. if (HAVE_THREADS && avctx->thread_opaque)
  859. ff_thread_free(avctx);
  860. if (avctx->codec && avctx->codec->close)
  861. avctx->codec->close(avctx);
  862. avcodec_default_free_buffers(avctx);
  863. avctx->coded_frame = NULL;
  864. av_freep(&avctx->internal);
  865. if (avctx->codec && avctx->codec->priv_class)
  866. av_opt_free(avctx->priv_data);
  867. av_opt_free(avctx);
  868. av_freep(&avctx->priv_data);
  869. if(avctx->codec && avctx->codec->encode)
  870. av_freep(&avctx->extradata);
  871. avctx->codec = NULL;
  872. avctx->active_thread_type = 0;
  873. entangled_thread_counter--;
  874. /* Release any user-supplied mutex. */
  875. if (ff_lockmgr_cb) {
  876. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  877. }
  878. return 0;
  879. }
  880. AVCodec *avcodec_find_encoder(enum CodecID id)
  881. {
  882. AVCodec *p, *experimental=NULL;
  883. p = first_avcodec;
  884. while (p) {
  885. if (p->encode != NULL && p->id == id) {
  886. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  887. experimental = p;
  888. } else
  889. return p;
  890. }
  891. p = p->next;
  892. }
  893. return experimental;
  894. }
  895. AVCodec *avcodec_find_encoder_by_name(const char *name)
  896. {
  897. AVCodec *p;
  898. if (!name)
  899. return NULL;
  900. p = first_avcodec;
  901. while (p) {
  902. if (p->encode != NULL && strcmp(name,p->name) == 0)
  903. return p;
  904. p = p->next;
  905. }
  906. return NULL;
  907. }
  908. AVCodec *avcodec_find_decoder(enum CodecID id)
  909. {
  910. AVCodec *p;
  911. p = first_avcodec;
  912. while (p) {
  913. if (p->decode != NULL && p->id == id)
  914. return p;
  915. p = p->next;
  916. }
  917. return NULL;
  918. }
  919. AVCodec *avcodec_find_decoder_by_name(const char *name)
  920. {
  921. AVCodec *p;
  922. if (!name)
  923. return NULL;
  924. p = first_avcodec;
  925. while (p) {
  926. if (p->decode != NULL && strcmp(name,p->name) == 0)
  927. return p;
  928. p = p->next;
  929. }
  930. return NULL;
  931. }
  932. static int get_bit_rate(AVCodecContext *ctx)
  933. {
  934. int bit_rate;
  935. int bits_per_sample;
  936. switch(ctx->codec_type) {
  937. case AVMEDIA_TYPE_VIDEO:
  938. case AVMEDIA_TYPE_DATA:
  939. case AVMEDIA_TYPE_SUBTITLE:
  940. case AVMEDIA_TYPE_ATTACHMENT:
  941. bit_rate = ctx->bit_rate;
  942. break;
  943. case AVMEDIA_TYPE_AUDIO:
  944. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  945. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  946. break;
  947. default:
  948. bit_rate = 0;
  949. break;
  950. }
  951. return bit_rate;
  952. }
  953. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  954. {
  955. int i, len, ret = 0;
  956. for (i = 0; i < 4; i++) {
  957. len = snprintf(buf, buf_size,
  958. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  959. buf += len;
  960. buf_size = buf_size > len ? buf_size - len : 0;
  961. ret += len;
  962. codec_tag>>=8;
  963. }
  964. return ret;
  965. }
  966. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  967. {
  968. const char *codec_name;
  969. const char *profile = NULL;
  970. AVCodec *p;
  971. char buf1[32];
  972. int bitrate;
  973. AVRational display_aspect_ratio;
  974. if (encode)
  975. p = avcodec_find_encoder(enc->codec_id);
  976. else
  977. p = avcodec_find_decoder(enc->codec_id);
  978. if (p) {
  979. codec_name = p->name;
  980. profile = av_get_profile_name(p, enc->profile);
  981. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  982. /* fake mpeg2 transport stream codec (currently not
  983. registered) */
  984. codec_name = "mpeg2ts";
  985. } else if (enc->codec_name[0] != '\0') {
  986. codec_name = enc->codec_name;
  987. } else {
  988. /* output avi tags */
  989. char tag_buf[32];
  990. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  991. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  992. codec_name = buf1;
  993. }
  994. switch(enc->codec_type) {
  995. case AVMEDIA_TYPE_VIDEO:
  996. snprintf(buf, buf_size,
  997. "Video: %s%s",
  998. codec_name, enc->mb_decision ? " (hq)" : "");
  999. if (profile)
  1000. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1001. " (%s)", profile);
  1002. if (enc->pix_fmt != PIX_FMT_NONE) {
  1003. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1004. ", %s",
  1005. av_get_pix_fmt_name(enc->pix_fmt));
  1006. }
  1007. if (enc->width) {
  1008. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1009. ", %dx%d",
  1010. enc->width, enc->height);
  1011. if (enc->sample_aspect_ratio.num) {
  1012. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1013. enc->width*enc->sample_aspect_ratio.num,
  1014. enc->height*enc->sample_aspect_ratio.den,
  1015. 1024*1024);
  1016. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1017. " [PAR %d:%d DAR %d:%d]",
  1018. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1019. display_aspect_ratio.num, display_aspect_ratio.den);
  1020. }
  1021. if(av_log_get_level() >= AV_LOG_DEBUG){
  1022. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1023. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1024. ", %d/%d",
  1025. enc->time_base.num/g, enc->time_base.den/g);
  1026. }
  1027. }
  1028. if (encode) {
  1029. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1030. ", q=%d-%d", enc->qmin, enc->qmax);
  1031. }
  1032. break;
  1033. case AVMEDIA_TYPE_AUDIO:
  1034. snprintf(buf, buf_size,
  1035. "Audio: %s",
  1036. codec_name);
  1037. if (profile)
  1038. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1039. " (%s)", profile);
  1040. if (enc->sample_rate) {
  1041. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1042. ", %d Hz", enc->sample_rate);
  1043. }
  1044. av_strlcat(buf, ", ", buf_size);
  1045. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1046. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1047. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1048. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1049. }
  1050. break;
  1051. case AVMEDIA_TYPE_DATA:
  1052. snprintf(buf, buf_size, "Data: %s", codec_name);
  1053. break;
  1054. case AVMEDIA_TYPE_SUBTITLE:
  1055. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1056. break;
  1057. case AVMEDIA_TYPE_ATTACHMENT:
  1058. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1059. break;
  1060. default:
  1061. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1062. return;
  1063. }
  1064. if (encode) {
  1065. if (enc->flags & CODEC_FLAG_PASS1)
  1066. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1067. ", pass 1");
  1068. if (enc->flags & CODEC_FLAG_PASS2)
  1069. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1070. ", pass 2");
  1071. }
  1072. bitrate = get_bit_rate(enc);
  1073. if (bitrate != 0) {
  1074. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1075. ", %d kb/s", bitrate / 1000);
  1076. }
  1077. }
  1078. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1079. {
  1080. const AVProfile *p;
  1081. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1082. return NULL;
  1083. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1084. if (p->profile == profile)
  1085. return p->name;
  1086. return NULL;
  1087. }
  1088. unsigned avcodec_version( void )
  1089. {
  1090. return LIBAVCODEC_VERSION_INT;
  1091. }
  1092. const char *avcodec_configuration(void)
  1093. {
  1094. return LIBAV_CONFIGURATION;
  1095. }
  1096. const char *avcodec_license(void)
  1097. {
  1098. #define LICENSE_PREFIX "libavcodec license: "
  1099. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1100. }
  1101. void avcodec_flush_buffers(AVCodecContext *avctx)
  1102. {
  1103. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1104. ff_thread_flush(avctx);
  1105. else if(avctx->codec->flush)
  1106. avctx->codec->flush(avctx);
  1107. }
  1108. static void video_free_buffers(AVCodecContext *s)
  1109. {
  1110. AVCodecInternal *avci = s->internal;
  1111. int i, j;
  1112. if (!avci->buffer)
  1113. return;
  1114. if (avci->buffer_count)
  1115. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1116. avci->buffer_count);
  1117. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1118. InternalBuffer *buf = &avci->buffer[i];
  1119. for(j=0; j<4; j++){
  1120. av_freep(&buf->base[j]);
  1121. buf->data[j]= NULL;
  1122. }
  1123. }
  1124. av_freep(&avci->buffer);
  1125. avci->buffer_count=0;
  1126. }
  1127. static void audio_free_buffers(AVCodecContext *avctx)
  1128. {
  1129. AVCodecInternal *avci = avctx->internal;
  1130. InternalBuffer *buf;
  1131. if (!avci->buffer)
  1132. return;
  1133. buf = avci->buffer;
  1134. if (buf->extended_data) {
  1135. av_free(buf->extended_data[0]);
  1136. if (buf->extended_data != buf->data)
  1137. av_free(buf->extended_data);
  1138. }
  1139. av_freep(&avci->buffer);
  1140. }
  1141. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1142. {
  1143. switch (avctx->codec_type) {
  1144. case AVMEDIA_TYPE_VIDEO:
  1145. video_free_buffers(avctx);
  1146. break;
  1147. case AVMEDIA_TYPE_AUDIO:
  1148. audio_free_buffers(avctx);
  1149. break;
  1150. default:
  1151. break;
  1152. }
  1153. }
  1154. #if FF_API_OLD_FF_PICT_TYPES
  1155. char av_get_pict_type_char(int pict_type){
  1156. return av_get_picture_type_char(pict_type);
  1157. }
  1158. #endif
  1159. int av_get_bits_per_sample(enum CodecID codec_id){
  1160. switch(codec_id){
  1161. case CODEC_ID_ADPCM_SBPRO_2:
  1162. return 2;
  1163. case CODEC_ID_ADPCM_SBPRO_3:
  1164. return 3;
  1165. case CODEC_ID_ADPCM_SBPRO_4:
  1166. case CODEC_ID_ADPCM_CT:
  1167. case CODEC_ID_ADPCM_IMA_WAV:
  1168. case CODEC_ID_ADPCM_IMA_QT:
  1169. case CODEC_ID_ADPCM_SWF:
  1170. case CODEC_ID_ADPCM_MS:
  1171. case CODEC_ID_ADPCM_YAMAHA:
  1172. case CODEC_ID_ADPCM_G722:
  1173. return 4;
  1174. case CODEC_ID_PCM_ALAW:
  1175. case CODEC_ID_PCM_MULAW:
  1176. case CODEC_ID_PCM_S8:
  1177. case CODEC_ID_PCM_U8:
  1178. case CODEC_ID_PCM_ZORK:
  1179. return 8;
  1180. case CODEC_ID_PCM_S16BE:
  1181. case CODEC_ID_PCM_S16LE:
  1182. case CODEC_ID_PCM_S16LE_PLANAR:
  1183. case CODEC_ID_PCM_U16BE:
  1184. case CODEC_ID_PCM_U16LE:
  1185. return 16;
  1186. case CODEC_ID_PCM_S24DAUD:
  1187. case CODEC_ID_PCM_S24BE:
  1188. case CODEC_ID_PCM_S24LE:
  1189. case CODEC_ID_PCM_U24BE:
  1190. case CODEC_ID_PCM_U24LE:
  1191. return 24;
  1192. case CODEC_ID_PCM_S32BE:
  1193. case CODEC_ID_PCM_S32LE:
  1194. case CODEC_ID_PCM_U32BE:
  1195. case CODEC_ID_PCM_U32LE:
  1196. case CODEC_ID_PCM_F32BE:
  1197. case CODEC_ID_PCM_F32LE:
  1198. return 32;
  1199. case CODEC_ID_PCM_F64BE:
  1200. case CODEC_ID_PCM_F64LE:
  1201. return 64;
  1202. default:
  1203. return 0;
  1204. }
  1205. }
  1206. #if FF_API_OLD_SAMPLE_FMT
  1207. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1208. return av_get_bytes_per_sample(sample_fmt) << 3;
  1209. }
  1210. #endif
  1211. #if !HAVE_THREADS
  1212. int ff_thread_init(AVCodecContext *s){
  1213. return -1;
  1214. }
  1215. #endif
  1216. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1217. {
  1218. unsigned int n = 0;
  1219. while(v >= 0xff) {
  1220. *s++ = 0xff;
  1221. v -= 0xff;
  1222. n++;
  1223. }
  1224. *s = v;
  1225. n++;
  1226. return n;
  1227. }
  1228. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1229. int i;
  1230. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1231. return i;
  1232. }
  1233. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1234. {
  1235. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
  1236. "version to the newest one from Git. If the problem still "
  1237. "occurs, it means that your file has a feature which has not "
  1238. "been implemented.\n", feature);
  1239. if(want_sample)
  1240. av_log_ask_for_sample(avc, NULL);
  1241. }
  1242. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1243. {
  1244. va_list argument_list;
  1245. va_start(argument_list, msg);
  1246. if (msg)
  1247. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1248. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1249. "of this file to ftp://upload.libav.org/incoming/ "
  1250. "and contact the libav-devel mailing list.\n");
  1251. va_end(argument_list);
  1252. }
  1253. static AVHWAccel *first_hwaccel = NULL;
  1254. void av_register_hwaccel(AVHWAccel *hwaccel)
  1255. {
  1256. AVHWAccel **p = &first_hwaccel;
  1257. while (*p)
  1258. p = &(*p)->next;
  1259. *p = hwaccel;
  1260. hwaccel->next = NULL;
  1261. }
  1262. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1263. {
  1264. return hwaccel ? hwaccel->next : first_hwaccel;
  1265. }
  1266. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1267. {
  1268. AVHWAccel *hwaccel=NULL;
  1269. while((hwaccel= av_hwaccel_next(hwaccel))){
  1270. if ( hwaccel->id == codec_id
  1271. && hwaccel->pix_fmt == pix_fmt)
  1272. return hwaccel;
  1273. }
  1274. return NULL;
  1275. }
  1276. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1277. {
  1278. if (ff_lockmgr_cb) {
  1279. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1280. return -1;
  1281. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1282. return -1;
  1283. }
  1284. ff_lockmgr_cb = cb;
  1285. if (ff_lockmgr_cb) {
  1286. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1287. return -1;
  1288. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1289. return -1;
  1290. }
  1291. return 0;
  1292. }
  1293. int avpriv_lock_avformat(void)
  1294. {
  1295. if (ff_lockmgr_cb) {
  1296. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1297. return -1;
  1298. }
  1299. return 0;
  1300. }
  1301. int avpriv_unlock_avformat(void)
  1302. {
  1303. if (ff_lockmgr_cb) {
  1304. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1305. return -1;
  1306. }
  1307. return 0;
  1308. }
  1309. unsigned int avpriv_toupper4(unsigned int x)
  1310. {
  1311. return toupper( x &0xFF)
  1312. + (toupper((x>>8 )&0xFF)<<8 )
  1313. + (toupper((x>>16)&0xFF)<<16)
  1314. + (toupper((x>>24)&0xFF)<<24);
  1315. }
  1316. #if !HAVE_THREADS
  1317. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1318. {
  1319. f->owner = avctx;
  1320. return avctx->get_buffer(avctx, f);
  1321. }
  1322. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1323. {
  1324. f->owner->release_buffer(f->owner, f);
  1325. }
  1326. void ff_thread_finish_setup(AVCodecContext *avctx)
  1327. {
  1328. }
  1329. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1330. {
  1331. }
  1332. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1333. {
  1334. }
  1335. #endif
  1336. #if FF_API_THREAD_INIT
  1337. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1338. {
  1339. s->thread_count = thread_count;
  1340. return ff_thread_init(s);
  1341. }
  1342. #endif
  1343. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1344. {
  1345. if (codec_id <= CODEC_ID_NONE)
  1346. return AVMEDIA_TYPE_UNKNOWN;
  1347. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1348. return AVMEDIA_TYPE_VIDEO;
  1349. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1350. return AVMEDIA_TYPE_AUDIO;
  1351. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1352. return AVMEDIA_TYPE_SUBTITLE;
  1353. return AVMEDIA_TYPE_UNKNOWN;
  1354. }