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.

1481 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_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  551. avctx->codec->max_lowres);
  552. ret = AVERROR(EINVAL);
  553. goto free_and_end;
  554. }
  555. if (avctx->codec->encode) {
  556. int i;
  557. if (avctx->codec->sample_fmts) {
  558. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  559. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  560. break;
  561. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  562. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  563. ret = AVERROR(EINVAL);
  564. goto free_and_end;
  565. }
  566. }
  567. if (avctx->codec->supported_samplerates) {
  568. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  569. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  570. break;
  571. if (avctx->codec->supported_samplerates[i] == 0) {
  572. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  573. ret = AVERROR(EINVAL);
  574. goto free_and_end;
  575. }
  576. }
  577. if (avctx->codec->channel_layouts) {
  578. if (!avctx->channel_layout) {
  579. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  580. } else {
  581. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  582. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  583. break;
  584. if (avctx->codec->channel_layouts[i] == 0) {
  585. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  586. ret = AVERROR(EINVAL);
  587. goto free_and_end;
  588. }
  589. }
  590. }
  591. if (avctx->channel_layout && avctx->channels) {
  592. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  593. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  594. ret = AVERROR(EINVAL);
  595. goto free_and_end;
  596. }
  597. } else if (avctx->channel_layout) {
  598. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  599. }
  600. }
  601. avctx->pts_correction_num_faulty_pts =
  602. avctx->pts_correction_num_faulty_dts = 0;
  603. avctx->pts_correction_last_pts =
  604. avctx->pts_correction_last_dts = INT64_MIN;
  605. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  606. ret = avctx->codec->init(avctx);
  607. if (ret < 0) {
  608. goto free_and_end;
  609. }
  610. }
  611. ret=0;
  612. end:
  613. entangled_thread_counter--;
  614. /* Release any user-supplied mutex. */
  615. if (ff_lockmgr_cb) {
  616. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  617. }
  618. if (options) {
  619. av_dict_free(options);
  620. *options = tmp;
  621. }
  622. return ret;
  623. free_and_end:
  624. av_dict_free(&tmp);
  625. av_freep(&avctx->priv_data);
  626. avctx->codec= NULL;
  627. goto end;
  628. }
  629. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  630. const short *samples)
  631. {
  632. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  633. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  634. return -1;
  635. }
  636. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  637. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  638. avctx->frame_number++;
  639. return ret;
  640. }else
  641. return 0;
  642. }
  643. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  644. const AVFrame *pict)
  645. {
  646. if(buf_size < FF_MIN_BUFFER_SIZE){
  647. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  648. return -1;
  649. }
  650. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  651. return -1;
  652. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  653. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  654. avctx->frame_number++;
  655. emms_c(); //needed to avoid an emms_c() call before every return;
  656. return ret;
  657. }else
  658. return 0;
  659. }
  660. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  661. const AVSubtitle *sub)
  662. {
  663. int ret;
  664. if(sub->start_display_time) {
  665. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  666. return -1;
  667. }
  668. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  669. avctx->frame_number++;
  670. return ret;
  671. }
  672. /**
  673. * Attempt to guess proper monotonic timestamps for decoded video frames
  674. * which might have incorrect times. Input timestamps may wrap around, in
  675. * which case the output will as well.
  676. *
  677. * @param pts the pts field of the decoded AVPacket, as passed through
  678. * AVFrame.pkt_pts
  679. * @param dts the dts field of the decoded AVPacket
  680. * @return one of the input values, may be AV_NOPTS_VALUE
  681. */
  682. static int64_t guess_correct_pts(AVCodecContext *ctx,
  683. int64_t reordered_pts, int64_t dts)
  684. {
  685. int64_t pts = AV_NOPTS_VALUE;
  686. if (dts != AV_NOPTS_VALUE) {
  687. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  688. ctx->pts_correction_last_dts = dts;
  689. }
  690. if (reordered_pts != AV_NOPTS_VALUE) {
  691. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  692. ctx->pts_correction_last_pts = reordered_pts;
  693. }
  694. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  695. && reordered_pts != AV_NOPTS_VALUE)
  696. pts = reordered_pts;
  697. else
  698. pts = dts;
  699. return pts;
  700. }
  701. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  702. int *got_picture_ptr,
  703. AVPacket *avpkt)
  704. {
  705. int ret;
  706. *got_picture_ptr= 0;
  707. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  708. return -1;
  709. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  710. av_packet_split_side_data(avpkt);
  711. avctx->pkt = avpkt;
  712. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  713. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  714. avpkt);
  715. else {
  716. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  717. avpkt);
  718. picture->pkt_dts= avpkt->dts;
  719. if(!avctx->has_b_frames){
  720. picture->pkt_pos= avpkt->pos;
  721. }
  722. //FIXME these should be under if(!avctx->has_b_frames)
  723. if (!picture->sample_aspect_ratio.num)
  724. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  725. if (!picture->width)
  726. picture->width = avctx->width;
  727. if (!picture->height)
  728. picture->height = avctx->height;
  729. if (picture->format == PIX_FMT_NONE)
  730. picture->format = avctx->pix_fmt;
  731. }
  732. emms_c(); //needed to avoid an emms_c() call before every return;
  733. if (*got_picture_ptr){
  734. avctx->frame_number++;
  735. picture->best_effort_timestamp = guess_correct_pts(avctx,
  736. picture->pkt_pts,
  737. picture->pkt_dts);
  738. }
  739. }else
  740. ret= 0;
  741. return ret;
  742. }
  743. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  744. int *frame_size_ptr,
  745. AVPacket *avpkt)
  746. {
  747. int ret;
  748. avctx->pkt = avpkt;
  749. if (!avpkt->data && avpkt->size) {
  750. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  751. return AVERROR(EINVAL);
  752. }
  753. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  754. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  755. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  756. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  757. return -1;
  758. }
  759. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  760. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  761. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  762. return -1;
  763. }
  764. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  765. avctx->frame_number++;
  766. }else{
  767. ret= 0;
  768. *frame_size_ptr=0;
  769. }
  770. return ret;
  771. }
  772. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  773. int *got_sub_ptr,
  774. AVPacket *avpkt)
  775. {
  776. int ret;
  777. avctx->pkt = avpkt;
  778. *got_sub_ptr = 0;
  779. avcodec_get_subtitle_defaults(sub);
  780. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  781. if (*got_sub_ptr)
  782. avctx->frame_number++;
  783. return ret;
  784. }
  785. void avsubtitle_free(AVSubtitle *sub)
  786. {
  787. int i;
  788. for (i = 0; i < sub->num_rects; i++)
  789. {
  790. av_freep(&sub->rects[i]->pict.data[0]);
  791. av_freep(&sub->rects[i]->pict.data[1]);
  792. av_freep(&sub->rects[i]->pict.data[2]);
  793. av_freep(&sub->rects[i]->pict.data[3]);
  794. av_freep(&sub->rects[i]->text);
  795. av_freep(&sub->rects[i]->ass);
  796. av_freep(&sub->rects[i]);
  797. }
  798. av_freep(&sub->rects);
  799. memset(sub, 0, sizeof(AVSubtitle));
  800. }
  801. av_cold int avcodec_close(AVCodecContext *avctx)
  802. {
  803. /* If there is a user-supplied mutex locking routine, call it. */
  804. if (ff_lockmgr_cb) {
  805. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  806. return -1;
  807. }
  808. entangled_thread_counter++;
  809. if(entangled_thread_counter != 1){
  810. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  811. entangled_thread_counter--;
  812. return -1;
  813. }
  814. if (HAVE_THREADS && avctx->thread_opaque)
  815. ff_thread_free(avctx);
  816. if (avctx->codec && avctx->codec->close)
  817. avctx->codec->close(avctx);
  818. avcodec_default_free_buffers(avctx);
  819. avctx->coded_frame = NULL;
  820. if (avctx->codec && avctx->codec->priv_class)
  821. av_opt_free(avctx->priv_data);
  822. av_opt_free(avctx);
  823. av_freep(&avctx->priv_data);
  824. if(avctx->codec && avctx->codec->encode)
  825. av_freep(&avctx->extradata);
  826. avctx->codec = NULL;
  827. avctx->active_thread_type = 0;
  828. entangled_thread_counter--;
  829. /* Release any user-supplied mutex. */
  830. if (ff_lockmgr_cb) {
  831. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  832. }
  833. return 0;
  834. }
  835. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  836. {
  837. switch(id){
  838. case CODEC_ID_G723_1_DEPRECATED : return CODEC_ID_G723_1;
  839. case CODEC_ID_G729_DEPRECATED : return CODEC_ID_G729;
  840. case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  841. default : return id;
  842. }
  843. }
  844. AVCodec *avcodec_find_encoder(enum CodecID id)
  845. {
  846. AVCodec *p, *experimental=NULL;
  847. p = first_avcodec;
  848. id= remap_deprecated_codec_id(id);
  849. while (p) {
  850. if (p->encode != NULL && p->id == id) {
  851. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  852. experimental = p;
  853. } else
  854. return p;
  855. }
  856. p = p->next;
  857. }
  858. return experimental;
  859. }
  860. AVCodec *avcodec_find_encoder_by_name(const char *name)
  861. {
  862. AVCodec *p;
  863. if (!name)
  864. return NULL;
  865. p = first_avcodec;
  866. while (p) {
  867. if (p->encode != NULL && strcmp(name,p->name) == 0)
  868. return p;
  869. p = p->next;
  870. }
  871. return NULL;
  872. }
  873. AVCodec *avcodec_find_decoder(enum CodecID id)
  874. {
  875. AVCodec *p, *experimental=NULL;
  876. p = first_avcodec;
  877. id= remap_deprecated_codec_id(id);
  878. while (p) {
  879. if (p->decode != NULL && p->id == id) {
  880. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  881. experimental = p;
  882. } else
  883. return p;
  884. }
  885. p = p->next;
  886. }
  887. return experimental;
  888. }
  889. AVCodec *avcodec_find_decoder_by_name(const char *name)
  890. {
  891. AVCodec *p;
  892. if (!name)
  893. return NULL;
  894. p = first_avcodec;
  895. while (p) {
  896. if (p->decode != NULL && strcmp(name,p->name) == 0)
  897. return p;
  898. p = p->next;
  899. }
  900. return NULL;
  901. }
  902. static int get_bit_rate(AVCodecContext *ctx)
  903. {
  904. int bit_rate;
  905. int bits_per_sample;
  906. switch(ctx->codec_type) {
  907. case AVMEDIA_TYPE_VIDEO:
  908. case AVMEDIA_TYPE_DATA:
  909. case AVMEDIA_TYPE_SUBTITLE:
  910. case AVMEDIA_TYPE_ATTACHMENT:
  911. bit_rate = ctx->bit_rate;
  912. break;
  913. case AVMEDIA_TYPE_AUDIO:
  914. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  915. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  916. break;
  917. default:
  918. bit_rate = 0;
  919. break;
  920. }
  921. return bit_rate;
  922. }
  923. const char *avcodec_get_name(enum CodecID id)
  924. {
  925. AVCodec *codec;
  926. #if !CONFIG_SMALL
  927. switch (id) {
  928. #include "libavcodec/codec_names.h"
  929. }
  930. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  931. #endif
  932. codec = avcodec_find_decoder(id);
  933. if (codec)
  934. return codec->name;
  935. codec = avcodec_find_encoder(id);
  936. if (codec)
  937. return codec->name;
  938. return "unknown_codec";
  939. }
  940. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  941. {
  942. int i, len, ret = 0;
  943. for (i = 0; i < 4; i++) {
  944. len = snprintf(buf, buf_size,
  945. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  946. buf += len;
  947. buf_size = buf_size > len ? buf_size - len : 0;
  948. ret += len;
  949. codec_tag>>=8;
  950. }
  951. return ret;
  952. }
  953. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  954. {
  955. const char *codec_type;
  956. const char *codec_name;
  957. const char *profile = NULL;
  958. AVCodec *p;
  959. int bitrate;
  960. AVRational display_aspect_ratio;
  961. if (!buf || buf_size <= 0)
  962. return;
  963. codec_type = av_get_media_type_string(enc->codec_type);
  964. codec_name = avcodec_get_name(enc->codec_id);
  965. if (enc->profile != FF_PROFILE_UNKNOWN) {
  966. p = encode ? avcodec_find_encoder(enc->codec_id) :
  967. avcodec_find_decoder(enc->codec_id);
  968. if (p)
  969. profile = av_get_profile_name(p, enc->profile);
  970. }
  971. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  972. codec_name, enc->mb_decision ? " (hq)" : "");
  973. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  974. if (profile)
  975. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  976. if (enc->codec_tag) {
  977. char tag_buf[32];
  978. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  979. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  980. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  981. }
  982. switch(enc->codec_type) {
  983. case AVMEDIA_TYPE_VIDEO:
  984. if (enc->pix_fmt != PIX_FMT_NONE) {
  985. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  986. ", %s",
  987. av_get_pix_fmt_name(enc->pix_fmt));
  988. }
  989. if (enc->width) {
  990. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  991. ", %dx%d",
  992. enc->width, enc->height);
  993. if (enc->sample_aspect_ratio.num) {
  994. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  995. enc->width*enc->sample_aspect_ratio.num,
  996. enc->height*enc->sample_aspect_ratio.den,
  997. 1024*1024);
  998. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  999. " [SAR %d:%d DAR %d:%d]",
  1000. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1001. display_aspect_ratio.num, display_aspect_ratio.den);
  1002. }
  1003. if(av_log_get_level() >= AV_LOG_DEBUG){
  1004. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1005. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1006. ", %d/%d",
  1007. enc->time_base.num/g, enc->time_base.den/g);
  1008. }
  1009. }
  1010. if (encode) {
  1011. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1012. ", q=%d-%d", enc->qmin, enc->qmax);
  1013. }
  1014. break;
  1015. case AVMEDIA_TYPE_AUDIO:
  1016. if (enc->sample_rate) {
  1017. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1018. ", %d Hz", enc->sample_rate);
  1019. }
  1020. av_strlcat(buf, ", ", buf_size);
  1021. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1022. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1023. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1024. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1025. }
  1026. break;
  1027. default:
  1028. return;
  1029. }
  1030. if (encode) {
  1031. if (enc->flags & CODEC_FLAG_PASS1)
  1032. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1033. ", pass 1");
  1034. if (enc->flags & CODEC_FLAG_PASS2)
  1035. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1036. ", pass 2");
  1037. }
  1038. bitrate = get_bit_rate(enc);
  1039. if (bitrate != 0) {
  1040. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1041. ", %d kb/s", bitrate / 1000);
  1042. }
  1043. }
  1044. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1045. {
  1046. const AVProfile *p;
  1047. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1048. return NULL;
  1049. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1050. if (p->profile == profile)
  1051. return p->name;
  1052. return NULL;
  1053. }
  1054. unsigned avcodec_version( void )
  1055. {
  1056. return LIBAVCODEC_VERSION_INT;
  1057. }
  1058. const char *avcodec_configuration(void)
  1059. {
  1060. return FFMPEG_CONFIGURATION;
  1061. }
  1062. const char *avcodec_license(void)
  1063. {
  1064. #define LICENSE_PREFIX "libavcodec license: "
  1065. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1066. }
  1067. void avcodec_flush_buffers(AVCodecContext *avctx)
  1068. {
  1069. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1070. ff_thread_flush(avctx);
  1071. else if(avctx->codec->flush)
  1072. avctx->codec->flush(avctx);
  1073. }
  1074. void avcodec_default_free_buffers(AVCodecContext *s){
  1075. int i, j;
  1076. if(s->internal_buffer==NULL) return;
  1077. if (s->internal_buffer_count)
  1078. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  1079. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1080. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  1081. for(j=0; j<4; j++){
  1082. av_freep(&buf->base[j]);
  1083. buf->data[j]= NULL;
  1084. }
  1085. }
  1086. av_freep(&s->internal_buffer);
  1087. s->internal_buffer_count=0;
  1088. }
  1089. #if FF_API_OLD_FF_PICT_TYPES
  1090. char av_get_pict_type_char(int pict_type){
  1091. return av_get_picture_type_char(pict_type);
  1092. }
  1093. #endif
  1094. int av_get_bits_per_sample(enum CodecID codec_id){
  1095. switch(codec_id){
  1096. case CODEC_ID_ADPCM_SBPRO_2:
  1097. return 2;
  1098. case CODEC_ID_ADPCM_SBPRO_3:
  1099. return 3;
  1100. case CODEC_ID_ADPCM_SBPRO_4:
  1101. case CODEC_ID_ADPCM_CT:
  1102. case CODEC_ID_ADPCM_IMA_WAV:
  1103. case CODEC_ID_ADPCM_IMA_QT:
  1104. case CODEC_ID_ADPCM_SWF:
  1105. case CODEC_ID_ADPCM_MS:
  1106. case CODEC_ID_ADPCM_YAMAHA:
  1107. return 4;
  1108. case CODEC_ID_ADPCM_G722:
  1109. case CODEC_ID_PCM_ALAW:
  1110. case CODEC_ID_PCM_MULAW:
  1111. case CODEC_ID_PCM_S8:
  1112. case CODEC_ID_PCM_U8:
  1113. case CODEC_ID_PCM_ZORK:
  1114. return 8;
  1115. case CODEC_ID_PCM_S16BE:
  1116. case CODEC_ID_PCM_S16LE:
  1117. case CODEC_ID_PCM_S16LE_PLANAR:
  1118. case CODEC_ID_PCM_U16BE:
  1119. case CODEC_ID_PCM_U16LE:
  1120. return 16;
  1121. case CODEC_ID_PCM_S24DAUD:
  1122. case CODEC_ID_PCM_S24BE:
  1123. case CODEC_ID_PCM_S24LE:
  1124. case CODEC_ID_PCM_U24BE:
  1125. case CODEC_ID_PCM_U24LE:
  1126. return 24;
  1127. case CODEC_ID_PCM_S32BE:
  1128. case CODEC_ID_PCM_S32LE:
  1129. case CODEC_ID_PCM_U32BE:
  1130. case CODEC_ID_PCM_U32LE:
  1131. case CODEC_ID_PCM_F32BE:
  1132. case CODEC_ID_PCM_F32LE:
  1133. return 32;
  1134. case CODEC_ID_PCM_F64BE:
  1135. case CODEC_ID_PCM_F64LE:
  1136. return 64;
  1137. default:
  1138. return 0;
  1139. }
  1140. }
  1141. #if FF_API_OLD_SAMPLE_FMT
  1142. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1143. return av_get_bytes_per_sample(sample_fmt) << 3;
  1144. }
  1145. #endif
  1146. #if !HAVE_THREADS
  1147. int ff_thread_init(AVCodecContext *s){
  1148. return -1;
  1149. }
  1150. #endif
  1151. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1152. {
  1153. unsigned int n = 0;
  1154. while(v >= 0xff) {
  1155. *s++ = 0xff;
  1156. v -= 0xff;
  1157. n++;
  1158. }
  1159. *s = v;
  1160. n++;
  1161. return n;
  1162. }
  1163. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1164. int i;
  1165. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1166. return i;
  1167. }
  1168. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1169. {
  1170. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1171. "version to the newest one from Git. If the problem still "
  1172. "occurs, it means that your file has a feature which has not "
  1173. "been implemented.\n", feature);
  1174. if(want_sample)
  1175. av_log_ask_for_sample(avc, NULL);
  1176. }
  1177. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1178. {
  1179. va_list argument_list;
  1180. va_start(argument_list, msg);
  1181. if (msg)
  1182. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1183. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1184. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1185. "and contact the ffmpeg-devel mailing list.\n");
  1186. va_end(argument_list);
  1187. }
  1188. static AVHWAccel *first_hwaccel = NULL;
  1189. void av_register_hwaccel(AVHWAccel *hwaccel)
  1190. {
  1191. AVHWAccel **p = &first_hwaccel;
  1192. while (*p)
  1193. p = &(*p)->next;
  1194. *p = hwaccel;
  1195. hwaccel->next = NULL;
  1196. }
  1197. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1198. {
  1199. return hwaccel ? hwaccel->next : first_hwaccel;
  1200. }
  1201. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1202. {
  1203. AVHWAccel *hwaccel=NULL;
  1204. while((hwaccel= av_hwaccel_next(hwaccel))){
  1205. if ( hwaccel->id == codec_id
  1206. && hwaccel->pix_fmt == pix_fmt)
  1207. return hwaccel;
  1208. }
  1209. return NULL;
  1210. }
  1211. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1212. {
  1213. if (ff_lockmgr_cb) {
  1214. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1215. return -1;
  1216. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1217. return -1;
  1218. }
  1219. ff_lockmgr_cb = cb;
  1220. if (ff_lockmgr_cb) {
  1221. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1222. return -1;
  1223. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1224. return -1;
  1225. }
  1226. return 0;
  1227. }
  1228. int avpriv_lock_avformat(void)
  1229. {
  1230. if (ff_lockmgr_cb) {
  1231. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1232. return -1;
  1233. }
  1234. return 0;
  1235. }
  1236. int avpriv_unlock_avformat(void)
  1237. {
  1238. if (ff_lockmgr_cb) {
  1239. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1240. return -1;
  1241. }
  1242. return 0;
  1243. }
  1244. unsigned int avpriv_toupper4(unsigned int x)
  1245. {
  1246. return toupper( x &0xFF)
  1247. + (toupper((x>>8 )&0xFF)<<8 )
  1248. + (toupper((x>>16)&0xFF)<<16)
  1249. + (toupper((x>>24)&0xFF)<<24);
  1250. }
  1251. #if !HAVE_THREADS
  1252. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1253. {
  1254. f->owner = avctx;
  1255. ff_init_buffer_info(avctx, f);
  1256. return avctx->get_buffer(avctx, f);
  1257. }
  1258. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1259. {
  1260. f->owner->release_buffer(f->owner, f);
  1261. }
  1262. void ff_thread_finish_setup(AVCodecContext *avctx)
  1263. {
  1264. }
  1265. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1266. {
  1267. }
  1268. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1269. {
  1270. }
  1271. #endif
  1272. #if FF_API_THREAD_INIT
  1273. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1274. {
  1275. s->thread_count = thread_count;
  1276. return ff_thread_init(s);
  1277. }
  1278. #endif
  1279. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1280. {
  1281. AVCodec *c= avcodec_find_decoder(codec_id);
  1282. if(!c)
  1283. c= avcodec_find_encoder(codec_id);
  1284. if(c)
  1285. return c->type;
  1286. if (codec_id <= CODEC_ID_NONE)
  1287. return AVMEDIA_TYPE_UNKNOWN;
  1288. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1289. return AVMEDIA_TYPE_VIDEO;
  1290. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1291. return AVMEDIA_TYPE_AUDIO;
  1292. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1293. return AVMEDIA_TYPE_SUBTITLE;
  1294. return AVMEDIA_TYPE_UNKNOWN;
  1295. }