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.

1480 lines
44KB

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