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.

1486 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. if(s->debug&FF_DEBUG_BUFFERS)
  353. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  354. return 0;
  355. }
  356. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  357. int i;
  358. InternalBuffer *buf, *last;
  359. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  360. assert(s->internal_buffer_count);
  361. if(s->internal_buffer){
  362. buf = NULL; /* avoids warning */
  363. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  364. buf= &((InternalBuffer*)s->internal_buffer)[i];
  365. if(buf->data[0] == pic->data[0])
  366. break;
  367. }
  368. assert(i < s->internal_buffer_count);
  369. s->internal_buffer_count--;
  370. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  371. FFSWAP(InternalBuffer, *buf, *last);
  372. }
  373. for(i=0; i<4; i++){
  374. pic->data[i]=NULL;
  375. // pic->base[i]=NULL;
  376. }
  377. //printf("R%X\n", pic->opaque);
  378. if(s->debug&FF_DEBUG_BUFFERS)
  379. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  380. }
  381. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  382. AVFrame temp_pic;
  383. int i;
  384. /* If no picture return a new buffer */
  385. if(pic->data[0] == NULL) {
  386. /* We will copy from buffer, so must be readable */
  387. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  388. return s->get_buffer(s, pic);
  389. }
  390. /* If internal buffer type return the same buffer */
  391. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  392. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  393. else pic->pkt_pts= AV_NOPTS_VALUE;
  394. pic->reordered_opaque= s->reordered_opaque;
  395. return 0;
  396. }
  397. /*
  398. * Not internal type and reget_buffer not overridden, emulate cr buffer
  399. */
  400. temp_pic = *pic;
  401. for(i = 0; i < 4; i++)
  402. pic->data[i] = pic->base[i] = NULL;
  403. pic->opaque = NULL;
  404. /* Allocate new frame */
  405. if (s->get_buffer(s, pic))
  406. return -1;
  407. /* Copy image data from old buffer to new buffer */
  408. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  409. s->height);
  410. s->release_buffer(s, &temp_pic); // Release old frame
  411. return 0;
  412. }
  413. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  414. int i;
  415. for(i=0; i<count; i++){
  416. int r= func(c, (char*)arg + i*size);
  417. if(ret) ret[i]= r;
  418. }
  419. return 0;
  420. }
  421. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  422. int i;
  423. for(i=0; i<count; i++){
  424. int r= func(c, arg, i, 0);
  425. if(ret) ret[i]= r;
  426. }
  427. return 0;
  428. }
  429. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  430. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  431. ++fmt;
  432. return fmt[0];
  433. }
  434. void avcodec_get_frame_defaults(AVFrame *pic){
  435. memset(pic, 0, sizeof(AVFrame));
  436. pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  437. pic->pkt_pos = -1;
  438. pic->key_frame= 1;
  439. pic->sample_aspect_ratio = (AVRational){0, 1};
  440. pic->format = -1; /* unknown */
  441. }
  442. AVFrame *avcodec_alloc_frame(void){
  443. AVFrame *pic= av_malloc(sizeof(AVFrame));
  444. if(pic==NULL) return NULL;
  445. avcodec_get_frame_defaults(pic);
  446. return pic;
  447. }
  448. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  449. {
  450. memset(sub, 0, sizeof(*sub));
  451. sub->pts = AV_NOPTS_VALUE;
  452. }
  453. #if FF_API_AVCODEC_OPEN
  454. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  455. {
  456. return avcodec_open2(avctx, codec, NULL);
  457. }
  458. #endif
  459. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  460. {
  461. int ret = 0;
  462. AVDictionary *tmp = NULL;
  463. if (options)
  464. av_dict_copy(&tmp, *options, 0);
  465. /* If there is a user-supplied mutex locking routine, call it. */
  466. if (ff_lockmgr_cb) {
  467. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  468. return -1;
  469. }
  470. entangled_thread_counter++;
  471. if(entangled_thread_counter != 1){
  472. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  473. ret = -1;
  474. goto end;
  475. }
  476. if(avctx->codec || !codec) {
  477. ret = AVERROR(EINVAL);
  478. goto end;
  479. }
  480. if (codec->priv_data_size > 0) {
  481. if(!avctx->priv_data){
  482. avctx->priv_data = av_mallocz(codec->priv_data_size);
  483. if (!avctx->priv_data) {
  484. ret = AVERROR(ENOMEM);
  485. goto end;
  486. }
  487. if (codec->priv_class) {
  488. *(AVClass**)avctx->priv_data= codec->priv_class;
  489. av_opt_set_defaults(avctx->priv_data);
  490. }
  491. }
  492. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  493. goto free_and_end;
  494. } else {
  495. avctx->priv_data = NULL;
  496. }
  497. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  498. goto free_and_end;
  499. if(avctx->coded_width && avctx->coded_height)
  500. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  501. else if(avctx->width && avctx->height)
  502. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  503. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  504. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  505. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  506. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  507. avcodec_set_dimensions(avctx, 0, 0);
  508. }
  509. /* if the decoder init function was already called previously,
  510. free the already allocated subtitle_header before overwriting it */
  511. if (codec->decode)
  512. av_freep(&avctx->subtitle_header);
  513. #define SANE_NB_CHANNELS 128U
  514. if (avctx->channels > SANE_NB_CHANNELS) {
  515. ret = AVERROR(EINVAL);
  516. goto free_and_end;
  517. }
  518. avctx->codec = codec;
  519. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  520. avctx->codec_id == CODEC_ID_NONE) {
  521. avctx->codec_type = codec->type;
  522. avctx->codec_id = codec->id;
  523. }
  524. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  525. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  526. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  527. ret = AVERROR(EINVAL);
  528. goto free_and_end;
  529. }
  530. avctx->frame_number = 0;
  531. #if FF_API_ER
  532. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
  533. avctx->error_recognition, avctx->err_recognition);
  534. switch(avctx->error_recognition){
  535. case FF_ER_EXPLODE : avctx->err_recognition |= AV_EF_EXPLODE | AV_EF_COMPLIANT | AV_EF_CAREFUL;
  536. break;
  537. case FF_ER_VERY_AGGRESSIVE:
  538. case FF_ER_AGGRESSIVE : avctx->err_recognition |= AV_EF_AGGRESSIVE;
  539. case FF_ER_COMPLIANT : avctx->err_recognition |= AV_EF_COMPLIANT;
  540. case FF_ER_CAREFUL : avctx->err_recognition |= AV_EF_CAREFUL;
  541. }
  542. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
  543. avctx->error_recognition, avctx->err_recognition);
  544. #endif
  545. if (!HAVE_THREADS)
  546. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  547. if (HAVE_THREADS && !avctx->thread_opaque) {
  548. ret = ff_thread_init(avctx);
  549. if (ret < 0) {
  550. goto free_and_end;
  551. }
  552. }
  553. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  554. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  555. avctx->codec->max_lowres);
  556. ret = AVERROR(EINVAL);
  557. goto free_and_end;
  558. }
  559. if (avctx->codec->encode) {
  560. int i;
  561. if (avctx->codec->sample_fmts) {
  562. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  563. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  564. break;
  565. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  566. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  567. ret = AVERROR(EINVAL);
  568. goto free_and_end;
  569. }
  570. }
  571. if (avctx->codec->supported_samplerates) {
  572. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  573. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  574. break;
  575. if (avctx->codec->supported_samplerates[i] == 0) {
  576. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  577. ret = AVERROR(EINVAL);
  578. goto free_and_end;
  579. }
  580. }
  581. if (avctx->codec->channel_layouts) {
  582. if (!avctx->channel_layout) {
  583. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  584. } else {
  585. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  586. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  587. break;
  588. if (avctx->codec->channel_layouts[i] == 0) {
  589. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  590. ret = AVERROR(EINVAL);
  591. goto free_and_end;
  592. }
  593. }
  594. }
  595. if (avctx->channel_layout && avctx->channels) {
  596. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  597. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  598. ret = AVERROR(EINVAL);
  599. goto free_and_end;
  600. }
  601. } else if (avctx->channel_layout) {
  602. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  603. }
  604. }
  605. avctx->pts_correction_num_faulty_pts =
  606. avctx->pts_correction_num_faulty_dts = 0;
  607. avctx->pts_correction_last_pts =
  608. avctx->pts_correction_last_dts = INT64_MIN;
  609. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  610. ret = avctx->codec->init(avctx);
  611. if (ret < 0) {
  612. goto free_and_end;
  613. }
  614. }
  615. ret=0;
  616. end:
  617. entangled_thread_counter--;
  618. /* Release any user-supplied mutex. */
  619. if (ff_lockmgr_cb) {
  620. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  621. }
  622. if (options) {
  623. av_dict_free(options);
  624. *options = tmp;
  625. }
  626. return ret;
  627. free_and_end:
  628. av_dict_free(&tmp);
  629. av_freep(&avctx->priv_data);
  630. avctx->codec= NULL;
  631. goto end;
  632. }
  633. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  634. const short *samples)
  635. {
  636. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  637. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  638. return -1;
  639. }
  640. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  641. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  642. avctx->frame_number++;
  643. return ret;
  644. }else
  645. return 0;
  646. }
  647. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  648. const AVFrame *pict)
  649. {
  650. if(buf_size < FF_MIN_BUFFER_SIZE){
  651. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  652. return -1;
  653. }
  654. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  655. return -1;
  656. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  657. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  658. avctx->frame_number++;
  659. emms_c(); //needed to avoid an emms_c() call before every return;
  660. return ret;
  661. }else
  662. return 0;
  663. }
  664. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  665. const AVSubtitle *sub)
  666. {
  667. int ret;
  668. if(sub->start_display_time) {
  669. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  670. return -1;
  671. }
  672. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  673. avctx->frame_number++;
  674. return ret;
  675. }
  676. /**
  677. * Attempt to guess proper monotonic timestamps for decoded video frames
  678. * which might have incorrect times. Input timestamps may wrap around, in
  679. * which case the output will as well.
  680. *
  681. * @param pts the pts field of the decoded AVPacket, as passed through
  682. * AVFrame.pkt_pts
  683. * @param dts the dts field of the decoded AVPacket
  684. * @return one of the input values, may be AV_NOPTS_VALUE
  685. */
  686. static int64_t guess_correct_pts(AVCodecContext *ctx,
  687. int64_t reordered_pts, int64_t dts)
  688. {
  689. int64_t pts = AV_NOPTS_VALUE;
  690. if (dts != AV_NOPTS_VALUE) {
  691. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  692. ctx->pts_correction_last_dts = dts;
  693. }
  694. if (reordered_pts != AV_NOPTS_VALUE) {
  695. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  696. ctx->pts_correction_last_pts = reordered_pts;
  697. }
  698. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  699. && reordered_pts != AV_NOPTS_VALUE)
  700. pts = reordered_pts;
  701. else
  702. pts = dts;
  703. return pts;
  704. }
  705. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  706. int *got_picture_ptr,
  707. AVPacket *avpkt)
  708. {
  709. int ret;
  710. *got_picture_ptr= 0;
  711. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  712. return -1;
  713. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  714. av_packet_split_side_data(avpkt);
  715. avctx->pkt = avpkt;
  716. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  717. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  718. avpkt);
  719. else {
  720. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  721. avpkt);
  722. picture->pkt_dts= avpkt->dts;
  723. if(!avctx->has_b_frames){
  724. picture->pkt_pos= avpkt->pos;
  725. }
  726. //FIXME these should be under if(!avctx->has_b_frames)
  727. if (!picture->sample_aspect_ratio.num)
  728. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  729. if (!picture->width)
  730. picture->width = avctx->width;
  731. if (!picture->height)
  732. picture->height = avctx->height;
  733. if (picture->format == PIX_FMT_NONE)
  734. picture->format = avctx->pix_fmt;
  735. }
  736. emms_c(); //needed to avoid an emms_c() call before every return;
  737. if (*got_picture_ptr){
  738. avctx->frame_number++;
  739. picture->best_effort_timestamp = guess_correct_pts(avctx,
  740. picture->pkt_pts,
  741. picture->pkt_dts);
  742. }
  743. }else
  744. ret= 0;
  745. return ret;
  746. }
  747. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  748. int *frame_size_ptr,
  749. AVPacket *avpkt)
  750. {
  751. int ret;
  752. avctx->pkt = avpkt;
  753. if (!avpkt->data && avpkt->size) {
  754. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  755. return AVERROR(EINVAL);
  756. }
  757. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  758. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  759. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  760. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  761. return -1;
  762. }
  763. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  764. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  765. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  766. return -1;
  767. }
  768. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  769. avctx->frame_number++;
  770. }else{
  771. ret= 0;
  772. *frame_size_ptr=0;
  773. }
  774. return ret;
  775. }
  776. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  777. int *got_sub_ptr,
  778. AVPacket *avpkt)
  779. {
  780. int ret;
  781. avctx->pkt = avpkt;
  782. *got_sub_ptr = 0;
  783. avcodec_get_subtitle_defaults(sub);
  784. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  785. if (*got_sub_ptr)
  786. avctx->frame_number++;
  787. return ret;
  788. }
  789. void avsubtitle_free(AVSubtitle *sub)
  790. {
  791. int i;
  792. for (i = 0; i < sub->num_rects; i++)
  793. {
  794. av_freep(&sub->rects[i]->pict.data[0]);
  795. av_freep(&sub->rects[i]->pict.data[1]);
  796. av_freep(&sub->rects[i]->pict.data[2]);
  797. av_freep(&sub->rects[i]->pict.data[3]);
  798. av_freep(&sub->rects[i]->text);
  799. av_freep(&sub->rects[i]->ass);
  800. av_freep(&sub->rects[i]);
  801. }
  802. av_freep(&sub->rects);
  803. memset(sub, 0, sizeof(AVSubtitle));
  804. }
  805. av_cold int avcodec_close(AVCodecContext *avctx)
  806. {
  807. /* If there is a user-supplied mutex locking routine, call it. */
  808. if (ff_lockmgr_cb) {
  809. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  810. return -1;
  811. }
  812. entangled_thread_counter++;
  813. if(entangled_thread_counter != 1){
  814. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  815. entangled_thread_counter--;
  816. return -1;
  817. }
  818. if (HAVE_THREADS && avctx->thread_opaque)
  819. ff_thread_free(avctx);
  820. if (avctx->codec && avctx->codec->close)
  821. avctx->codec->close(avctx);
  822. avcodec_default_free_buffers(avctx);
  823. avctx->coded_frame = NULL;
  824. if (avctx->codec && avctx->codec->priv_class)
  825. av_opt_free(avctx->priv_data);
  826. av_opt_free(avctx);
  827. av_freep(&avctx->priv_data);
  828. if(avctx->codec && avctx->codec->encode)
  829. av_freep(&avctx->extradata);
  830. avctx->codec = NULL;
  831. avctx->active_thread_type = 0;
  832. entangled_thread_counter--;
  833. /* Release any user-supplied mutex. */
  834. if (ff_lockmgr_cb) {
  835. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  836. }
  837. return 0;
  838. }
  839. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  840. {
  841. switch(id){
  842. case CODEC_ID_G723_1_DEPRECATED : return CODEC_ID_G723_1;
  843. case CODEC_ID_G729_DEPRECATED : return CODEC_ID_G729;
  844. case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  845. default : return id;
  846. }
  847. }
  848. AVCodec *avcodec_find_encoder(enum CodecID id)
  849. {
  850. AVCodec *p, *experimental=NULL;
  851. p = first_avcodec;
  852. id= remap_deprecated_codec_id(id);
  853. while (p) {
  854. if (p->encode != NULL && p->id == id) {
  855. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  856. experimental = p;
  857. } else
  858. return p;
  859. }
  860. p = p->next;
  861. }
  862. return experimental;
  863. }
  864. AVCodec *avcodec_find_encoder_by_name(const char *name)
  865. {
  866. AVCodec *p;
  867. if (!name)
  868. return NULL;
  869. p = first_avcodec;
  870. while (p) {
  871. if (p->encode != NULL && strcmp(name,p->name) == 0)
  872. return p;
  873. p = p->next;
  874. }
  875. return NULL;
  876. }
  877. AVCodec *avcodec_find_decoder(enum CodecID id)
  878. {
  879. AVCodec *p, *experimental=NULL;
  880. p = first_avcodec;
  881. id= remap_deprecated_codec_id(id);
  882. while (p) {
  883. if (p->decode != NULL && p->id == id) {
  884. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  885. experimental = p;
  886. } else
  887. return p;
  888. }
  889. p = p->next;
  890. }
  891. return experimental;
  892. }
  893. AVCodec *avcodec_find_decoder_by_name(const char *name)
  894. {
  895. AVCodec *p;
  896. if (!name)
  897. return NULL;
  898. p = first_avcodec;
  899. while (p) {
  900. if (p->decode != NULL && strcmp(name,p->name) == 0)
  901. return p;
  902. p = p->next;
  903. }
  904. return NULL;
  905. }
  906. static int get_bit_rate(AVCodecContext *ctx)
  907. {
  908. int bit_rate;
  909. int bits_per_sample;
  910. switch(ctx->codec_type) {
  911. case AVMEDIA_TYPE_VIDEO:
  912. case AVMEDIA_TYPE_DATA:
  913. case AVMEDIA_TYPE_SUBTITLE:
  914. case AVMEDIA_TYPE_ATTACHMENT:
  915. bit_rate = ctx->bit_rate;
  916. break;
  917. case AVMEDIA_TYPE_AUDIO:
  918. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  919. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  920. break;
  921. default:
  922. bit_rate = 0;
  923. break;
  924. }
  925. return bit_rate;
  926. }
  927. const char *avcodec_get_name(enum CodecID id)
  928. {
  929. AVCodec *codec;
  930. #if !CONFIG_SMALL
  931. switch (id) {
  932. #include "libavcodec/codec_names.h"
  933. }
  934. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  935. #endif
  936. codec = avcodec_find_decoder(id);
  937. if (codec)
  938. return codec->name;
  939. codec = avcodec_find_encoder(id);
  940. if (codec)
  941. return codec->name;
  942. return "unknown_codec";
  943. }
  944. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  945. {
  946. int i, len, ret = 0;
  947. for (i = 0; i < 4; i++) {
  948. len = snprintf(buf, buf_size,
  949. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  950. buf += len;
  951. buf_size = buf_size > len ? buf_size - len : 0;
  952. ret += len;
  953. codec_tag>>=8;
  954. }
  955. return ret;
  956. }
  957. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  958. {
  959. const char *codec_type;
  960. const char *codec_name;
  961. const char *profile = NULL;
  962. AVCodec *p;
  963. int bitrate;
  964. AVRational display_aspect_ratio;
  965. if (!buf || buf_size <= 0)
  966. return;
  967. codec_type = av_get_media_type_string(enc->codec_type);
  968. codec_name = avcodec_get_name(enc->codec_id);
  969. if (enc->profile != FF_PROFILE_UNKNOWN) {
  970. p = encode ? avcodec_find_encoder(enc->codec_id) :
  971. avcodec_find_decoder(enc->codec_id);
  972. if (p)
  973. profile = av_get_profile_name(p, enc->profile);
  974. }
  975. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  976. codec_name, enc->mb_decision ? " (hq)" : "");
  977. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  978. if (profile)
  979. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  980. if (enc->codec_tag) {
  981. char tag_buf[32];
  982. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  983. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  984. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  985. }
  986. switch(enc->codec_type) {
  987. case AVMEDIA_TYPE_VIDEO:
  988. if (enc->pix_fmt != PIX_FMT_NONE) {
  989. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  990. ", %s",
  991. av_get_pix_fmt_name(enc->pix_fmt));
  992. }
  993. if (enc->width) {
  994. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  995. ", %dx%d",
  996. enc->width, enc->height);
  997. if (enc->sample_aspect_ratio.num) {
  998. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  999. enc->width*enc->sample_aspect_ratio.num,
  1000. enc->height*enc->sample_aspect_ratio.den,
  1001. 1024*1024);
  1002. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1003. " [SAR %d:%d DAR %d:%d]",
  1004. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1005. display_aspect_ratio.num, display_aspect_ratio.den);
  1006. }
  1007. if(av_log_get_level() >= AV_LOG_DEBUG){
  1008. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1009. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1010. ", %d/%d",
  1011. enc->time_base.num/g, enc->time_base.den/g);
  1012. }
  1013. }
  1014. if (encode) {
  1015. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1016. ", q=%d-%d", enc->qmin, enc->qmax);
  1017. }
  1018. break;
  1019. case AVMEDIA_TYPE_AUDIO:
  1020. if (enc->sample_rate) {
  1021. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1022. ", %d Hz", enc->sample_rate);
  1023. }
  1024. av_strlcat(buf, ", ", buf_size);
  1025. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1026. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1027. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1028. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1029. }
  1030. break;
  1031. default:
  1032. return;
  1033. }
  1034. if (encode) {
  1035. if (enc->flags & CODEC_FLAG_PASS1)
  1036. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1037. ", pass 1");
  1038. if (enc->flags & CODEC_FLAG_PASS2)
  1039. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1040. ", pass 2");
  1041. }
  1042. bitrate = get_bit_rate(enc);
  1043. if (bitrate != 0) {
  1044. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1045. ", %d kb/s", bitrate / 1000);
  1046. }
  1047. }
  1048. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1049. {
  1050. const AVProfile *p;
  1051. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1052. return NULL;
  1053. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1054. if (p->profile == profile)
  1055. return p->name;
  1056. return NULL;
  1057. }
  1058. unsigned avcodec_version( void )
  1059. {
  1060. return LIBAVCODEC_VERSION_INT;
  1061. }
  1062. const char *avcodec_configuration(void)
  1063. {
  1064. return FFMPEG_CONFIGURATION;
  1065. }
  1066. const char *avcodec_license(void)
  1067. {
  1068. #define LICENSE_PREFIX "libavcodec license: "
  1069. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1070. }
  1071. void avcodec_flush_buffers(AVCodecContext *avctx)
  1072. {
  1073. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1074. ff_thread_flush(avctx);
  1075. else if(avctx->codec->flush)
  1076. avctx->codec->flush(avctx);
  1077. }
  1078. void avcodec_default_free_buffers(AVCodecContext *s){
  1079. int i, j;
  1080. if(s->internal_buffer==NULL) return;
  1081. if (s->internal_buffer_count)
  1082. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  1083. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1084. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  1085. for(j=0; j<4; j++){
  1086. av_freep(&buf->base[j]);
  1087. buf->data[j]= NULL;
  1088. }
  1089. }
  1090. av_freep(&s->internal_buffer);
  1091. s->internal_buffer_count=0;
  1092. }
  1093. #if FF_API_OLD_FF_PICT_TYPES
  1094. char av_get_pict_type_char(int pict_type){
  1095. return av_get_picture_type_char(pict_type);
  1096. }
  1097. #endif
  1098. int av_get_bits_per_sample(enum CodecID codec_id){
  1099. switch(codec_id){
  1100. case CODEC_ID_ADPCM_SBPRO_2:
  1101. return 2;
  1102. case CODEC_ID_ADPCM_SBPRO_3:
  1103. return 3;
  1104. case CODEC_ID_ADPCM_SBPRO_4:
  1105. case CODEC_ID_ADPCM_CT:
  1106. case CODEC_ID_ADPCM_IMA_WAV:
  1107. case CODEC_ID_ADPCM_IMA_QT:
  1108. case CODEC_ID_ADPCM_SWF:
  1109. case CODEC_ID_ADPCM_MS:
  1110. case CODEC_ID_ADPCM_YAMAHA:
  1111. return 4;
  1112. case CODEC_ID_ADPCM_G722:
  1113. case CODEC_ID_PCM_ALAW:
  1114. case CODEC_ID_PCM_MULAW:
  1115. case CODEC_ID_PCM_S8:
  1116. case CODEC_ID_PCM_U8:
  1117. case CODEC_ID_PCM_ZORK:
  1118. return 8;
  1119. case CODEC_ID_PCM_S16BE:
  1120. case CODEC_ID_PCM_S16LE:
  1121. case CODEC_ID_PCM_S16LE_PLANAR:
  1122. case CODEC_ID_PCM_U16BE:
  1123. case CODEC_ID_PCM_U16LE:
  1124. return 16;
  1125. case CODEC_ID_PCM_S24DAUD:
  1126. case CODEC_ID_PCM_S24BE:
  1127. case CODEC_ID_PCM_S24LE:
  1128. case CODEC_ID_PCM_U24BE:
  1129. case CODEC_ID_PCM_U24LE:
  1130. return 24;
  1131. case CODEC_ID_PCM_S32BE:
  1132. case CODEC_ID_PCM_S32LE:
  1133. case CODEC_ID_PCM_U32BE:
  1134. case CODEC_ID_PCM_U32LE:
  1135. case CODEC_ID_PCM_F32BE:
  1136. case CODEC_ID_PCM_F32LE:
  1137. return 32;
  1138. case CODEC_ID_PCM_F64BE:
  1139. case CODEC_ID_PCM_F64LE:
  1140. return 64;
  1141. default:
  1142. return 0;
  1143. }
  1144. }
  1145. #if FF_API_OLD_SAMPLE_FMT
  1146. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1147. return av_get_bytes_per_sample(sample_fmt) << 3;
  1148. }
  1149. #endif
  1150. #if !HAVE_THREADS
  1151. int ff_thread_init(AVCodecContext *s){
  1152. return -1;
  1153. }
  1154. #endif
  1155. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1156. {
  1157. unsigned int n = 0;
  1158. while(v >= 0xff) {
  1159. *s++ = 0xff;
  1160. v -= 0xff;
  1161. n++;
  1162. }
  1163. *s = v;
  1164. n++;
  1165. return n;
  1166. }
  1167. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1168. int i;
  1169. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1170. return i;
  1171. }
  1172. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1173. {
  1174. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1175. "version to the newest one from Git. If the problem still "
  1176. "occurs, it means that your file has a feature which has not "
  1177. "been implemented.\n", feature);
  1178. if(want_sample)
  1179. av_log_ask_for_sample(avc, NULL);
  1180. }
  1181. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1182. {
  1183. va_list argument_list;
  1184. va_start(argument_list, msg);
  1185. if (msg)
  1186. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1187. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1188. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1189. "and contact the ffmpeg-devel mailing list.\n");
  1190. va_end(argument_list);
  1191. }
  1192. static AVHWAccel *first_hwaccel = NULL;
  1193. void av_register_hwaccel(AVHWAccel *hwaccel)
  1194. {
  1195. AVHWAccel **p = &first_hwaccel;
  1196. while (*p)
  1197. p = &(*p)->next;
  1198. *p = hwaccel;
  1199. hwaccel->next = NULL;
  1200. }
  1201. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1202. {
  1203. return hwaccel ? hwaccel->next : first_hwaccel;
  1204. }
  1205. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1206. {
  1207. AVHWAccel *hwaccel=NULL;
  1208. while((hwaccel= av_hwaccel_next(hwaccel))){
  1209. if ( hwaccel->id == codec_id
  1210. && hwaccel->pix_fmt == pix_fmt)
  1211. return hwaccel;
  1212. }
  1213. return NULL;
  1214. }
  1215. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1216. {
  1217. if (ff_lockmgr_cb) {
  1218. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1219. return -1;
  1220. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1221. return -1;
  1222. }
  1223. ff_lockmgr_cb = cb;
  1224. if (ff_lockmgr_cb) {
  1225. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1226. return -1;
  1227. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1228. return -1;
  1229. }
  1230. return 0;
  1231. }
  1232. int avpriv_lock_avformat(void)
  1233. {
  1234. if (ff_lockmgr_cb) {
  1235. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1236. return -1;
  1237. }
  1238. return 0;
  1239. }
  1240. int avpriv_unlock_avformat(void)
  1241. {
  1242. if (ff_lockmgr_cb) {
  1243. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1244. return -1;
  1245. }
  1246. return 0;
  1247. }
  1248. unsigned int avpriv_toupper4(unsigned int x)
  1249. {
  1250. return toupper( x &0xFF)
  1251. + (toupper((x>>8 )&0xFF)<<8 )
  1252. + (toupper((x>>16)&0xFF)<<16)
  1253. + (toupper((x>>24)&0xFF)<<24);
  1254. }
  1255. #if !HAVE_THREADS
  1256. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1257. {
  1258. f->owner = avctx;
  1259. ff_init_buffer_info(avctx, f);
  1260. return avctx->get_buffer(avctx, f);
  1261. }
  1262. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1263. {
  1264. f->owner->release_buffer(f->owner, f);
  1265. }
  1266. void ff_thread_finish_setup(AVCodecContext *avctx)
  1267. {
  1268. }
  1269. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1270. {
  1271. }
  1272. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1273. {
  1274. }
  1275. #endif
  1276. #if FF_API_THREAD_INIT
  1277. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1278. {
  1279. s->thread_count = thread_count;
  1280. return ff_thread_init(s);
  1281. }
  1282. #endif
  1283. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1284. {
  1285. AVCodec *c= avcodec_find_decoder(codec_id);
  1286. if(!c)
  1287. c= avcodec_find_encoder(codec_id);
  1288. if(c)
  1289. return c->type;
  1290. if (codec_id <= CODEC_ID_NONE)
  1291. return AVMEDIA_TYPE_UNKNOWN;
  1292. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1293. return AVMEDIA_TYPE_VIDEO;
  1294. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1295. return AVMEDIA_TYPE_AUDIO;
  1296. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1297. return AVMEDIA_TYPE_SUBTITLE;
  1298. return AVMEDIA_TYPE_UNKNOWN;
  1299. }