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. #define INTERNAL_BUFFER_SIZE (32+1)
  110. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  111. int w_align= 1;
  112. int h_align= 1;
  113. switch(s->pix_fmt){
  114. case PIX_FMT_YUV420P:
  115. case PIX_FMT_YUYV422:
  116. case PIX_FMT_UYVY422:
  117. case PIX_FMT_YUV422P:
  118. case PIX_FMT_YUV440P:
  119. case PIX_FMT_YUV444P:
  120. case PIX_FMT_GRAY8:
  121. case PIX_FMT_GRAY16BE:
  122. case PIX_FMT_GRAY16LE:
  123. case PIX_FMT_YUVJ420P:
  124. case PIX_FMT_YUVJ422P:
  125. case PIX_FMT_YUVJ440P:
  126. case PIX_FMT_YUVJ444P:
  127. case PIX_FMT_YUVA420P:
  128. case PIX_FMT_YUV420P9LE:
  129. case PIX_FMT_YUV420P9BE:
  130. case PIX_FMT_YUV420P10LE:
  131. case PIX_FMT_YUV420P10BE:
  132. case PIX_FMT_YUV422P9LE:
  133. case PIX_FMT_YUV422P9BE:
  134. case PIX_FMT_YUV422P10LE:
  135. case PIX_FMT_YUV422P10BE:
  136. case PIX_FMT_YUV444P9LE:
  137. case PIX_FMT_YUV444P9BE:
  138. case PIX_FMT_YUV444P10LE:
  139. case PIX_FMT_YUV444P10BE:
  140. case PIX_FMT_GBR24P:
  141. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  142. h_align= 16;
  143. 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)
  144. h_align= 32; // interlaced is rounded up to 2 MBs
  145. break;
  146. case PIX_FMT_YUV411P:
  147. case PIX_FMT_UYYVYY411:
  148. w_align=32;
  149. h_align=8;
  150. break;
  151. case PIX_FMT_YUV410P:
  152. if(s->codec_id == CODEC_ID_SVQ1){
  153. w_align=64;
  154. h_align=64;
  155. }
  156. case PIX_FMT_RGB555:
  157. if(s->codec_id == CODEC_ID_RPZA){
  158. w_align=4;
  159. h_align=4;
  160. }
  161. case PIX_FMT_PAL8:
  162. case PIX_FMT_BGR8:
  163. case PIX_FMT_RGB8:
  164. if(s->codec_id == CODEC_ID_SMC){
  165. w_align=4;
  166. h_align=4;
  167. }
  168. break;
  169. case PIX_FMT_BGR24:
  170. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  171. w_align=4;
  172. h_align=4;
  173. }
  174. break;
  175. default:
  176. w_align= 1;
  177. h_align= 1;
  178. break;
  179. }
  180. *width = FFALIGN(*width , w_align);
  181. *height= FFALIGN(*height, h_align);
  182. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  183. *height+=2; // some of the optimized chroma MC reads one line too much
  184. // which is also done in mpeg decoders with lowres > 0
  185. linesize_align[0] =
  186. linesize_align[1] =
  187. linesize_align[2] =
  188. linesize_align[3] = STRIDE_ALIGN;
  189. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  190. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  191. //picture size unneccessarily in some cases. The solution here is not
  192. //pretty and better ideas are welcome!
  193. #if HAVE_MMX
  194. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  195. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  196. s->codec_id == CODEC_ID_VP6A || s->codec_id == CODEC_ID_DIRAC) {
  197. linesize_align[0] =
  198. linesize_align[1] =
  199. linesize_align[2] = 16;
  200. }
  201. #endif
  202. }
  203. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  204. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  205. int linesize_align[4];
  206. int align;
  207. avcodec_align_dimensions2(s, width, height, linesize_align);
  208. align = FFMAX(linesize_align[0], linesize_align[3]);
  209. linesize_align[1] <<= chroma_shift;
  210. linesize_align[2] <<= chroma_shift;
  211. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  212. *width=FFALIGN(*width, align);
  213. }
  214. void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
  215. {
  216. if (s->pkt) {
  217. pic->pkt_pts = s->pkt->pts;
  218. pic->pkt_pos = s->pkt->pos;
  219. } else {
  220. pic->pkt_pts = AV_NOPTS_VALUE;
  221. pic->pkt_pos = -1;
  222. }
  223. pic->reordered_opaque= s->reordered_opaque;
  224. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  225. pic->width = s->width;
  226. pic->height = s->height;
  227. pic->format = s->pix_fmt;
  228. }
  229. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  230. int i;
  231. int w= s->width;
  232. int h= s->height;
  233. InternalBuffer *buf;
  234. int *picture_number;
  235. AVCodecInternal *avci = s->internal;
  236. if(pic->data[0]!=NULL) {
  237. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  238. return -1;
  239. }
  240. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  241. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  242. return -1;
  243. }
  244. if(av_image_check_size(w, h, 0, s))
  245. return -1;
  246. if (!avci->buffer) {
  247. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  248. sizeof(InternalBuffer));
  249. }
  250. buf = &avci->buffer[avci->buffer_count];
  251. picture_number = &(avci->buffer[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  252. (*picture_number)++;
  253. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  254. if(s->active_thread_type&FF_THREAD_FRAME) {
  255. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  256. return -1;
  257. }
  258. for(i=0; i<4; i++){
  259. av_freep(&buf->base[i]);
  260. buf->data[i]= NULL;
  261. }
  262. }
  263. if(buf->base[0]){
  264. pic->age= *picture_number - buf->last_pic_num;
  265. buf->last_pic_num= *picture_number;
  266. }else{
  267. int h_chroma_shift, v_chroma_shift;
  268. int size[4] = {0};
  269. int tmpsize;
  270. int unaligned;
  271. AVPicture picture;
  272. int stride_align[4];
  273. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  274. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  275. avcodec_align_dimensions2(s, &w, &h, stride_align);
  276. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  277. w+= EDGE_WIDTH*2;
  278. h+= EDGE_WIDTH*2;
  279. }
  280. do {
  281. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  282. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  283. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  284. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  285. w += w & ~(w-1);
  286. unaligned = 0;
  287. for (i=0; i<4; i++){
  288. unaligned |= picture.linesize[i] % stride_align[i];
  289. }
  290. } while (unaligned);
  291. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  292. if (tmpsize < 0)
  293. return -1;
  294. for (i=0; i<3 && picture.data[i+1]; i++)
  295. size[i] = picture.data[i+1] - picture.data[i];
  296. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  297. buf->last_pic_num= -256*256*256*64;
  298. memset(buf->base, 0, sizeof(buf->base));
  299. memset(buf->data, 0, sizeof(buf->data));
  300. for(i=0; i<4 && size[i]; i++){
  301. const int h_shift= i==0 ? 0 : h_chroma_shift;
  302. const int v_shift= i==0 ? 0 : v_chroma_shift;
  303. buf->linesize[i]= picture.linesize[i];
  304. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  305. if(buf->base[i]==NULL) return -1;
  306. memset(buf->base[i], 128, size[i]);
  307. // no edge if EDGE EMU or not planar YUV
  308. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  309. buf->data[i] = buf->base[i];
  310. else
  311. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  312. }
  313. if(size[1] && !size[2])
  314. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  315. buf->width = s->width;
  316. buf->height = s->height;
  317. buf->pix_fmt= s->pix_fmt;
  318. pic->age= 256*256*256*64;
  319. }
  320. pic->type= FF_BUFFER_TYPE_INTERNAL;
  321. for(i=0; i<4; i++){
  322. pic->base[i]= buf->base[i];
  323. pic->data[i]= buf->data[i];
  324. pic->linesize[i]= buf->linesize[i];
  325. }
  326. avci->buffer_count++;
  327. if (s->pkt) {
  328. pic->pkt_pts = s->pkt->pts;
  329. pic->pkt_pos = s->pkt->pos;
  330. } else {
  331. pic->pkt_pts = AV_NOPTS_VALUE;
  332. pic->pkt_pos = -1;
  333. }
  334. pic->reordered_opaque= s->reordered_opaque;
  335. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  336. pic->width = s->width;
  337. pic->height = s->height;
  338. pic->format = s->pix_fmt;
  339. if(s->debug&FF_DEBUG_BUFFERS)
  340. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  341. "buffers used\n", pic, avci->buffer_count);
  342. return 0;
  343. }
  344. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  345. int i;
  346. InternalBuffer *buf, *last;
  347. AVCodecInternal *avci = s->internal;
  348. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  349. assert(avci->buffer_count);
  350. if (avci->buffer) {
  351. buf = NULL; /* avoids warning */
  352. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  353. buf = &avci->buffer[i];
  354. if (buf->data[0] == pic->data[0])
  355. break;
  356. }
  357. assert(i < avci->buffer_count);
  358. avci->buffer_count--;
  359. last = &avci->buffer[avci->buffer_count];
  360. FFSWAP(InternalBuffer, *buf, *last);
  361. }
  362. for(i=0; i<4; i++){
  363. pic->data[i]=NULL;
  364. // pic->base[i]=NULL;
  365. }
  366. //printf("R%X\n", pic->opaque);
  367. if(s->debug&FF_DEBUG_BUFFERS)
  368. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  369. "buffers used\n", pic, avci->buffer_count);
  370. }
  371. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  372. AVFrame temp_pic;
  373. int i;
  374. /* If no picture return a new buffer */
  375. if(pic->data[0] == NULL) {
  376. /* We will copy from buffer, so must be readable */
  377. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  378. return s->get_buffer(s, pic);
  379. }
  380. /* If internal buffer type return the same buffer */
  381. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  382. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  383. else pic->pkt_pts= AV_NOPTS_VALUE;
  384. pic->reordered_opaque= s->reordered_opaque;
  385. return 0;
  386. }
  387. /*
  388. * Not internal type and reget_buffer not overridden, emulate cr buffer
  389. */
  390. temp_pic = *pic;
  391. for(i = 0; i < 4; i++)
  392. pic->data[i] = pic->base[i] = NULL;
  393. pic->opaque = NULL;
  394. /* Allocate new frame */
  395. if (s->get_buffer(s, pic))
  396. return -1;
  397. /* Copy image data from old buffer to new buffer */
  398. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  399. s->height);
  400. s->release_buffer(s, &temp_pic); // Release old frame
  401. return 0;
  402. }
  403. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  404. int i;
  405. for(i=0; i<count; i++){
  406. int r= func(c, (char*)arg + i*size);
  407. if(ret) ret[i]= r;
  408. }
  409. return 0;
  410. }
  411. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  412. int i;
  413. for(i=0; i<count; i++){
  414. int r= func(c, arg, i, 0);
  415. if(ret) ret[i]= r;
  416. }
  417. return 0;
  418. }
  419. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  420. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  421. ++fmt;
  422. return fmt[0];
  423. }
  424. void avcodec_get_frame_defaults(AVFrame *pic){
  425. memset(pic, 0, sizeof(AVFrame));
  426. pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  427. pic->pkt_pos = -1;
  428. pic->key_frame= 1;
  429. pic->sample_aspect_ratio = (AVRational){0, 1};
  430. pic->format = -1; /* unknown */
  431. }
  432. AVFrame *avcodec_alloc_frame(void){
  433. AVFrame *pic= av_malloc(sizeof(AVFrame));
  434. if(pic==NULL) return NULL;
  435. avcodec_get_frame_defaults(pic);
  436. return pic;
  437. }
  438. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  439. {
  440. memset(sub, 0, sizeof(*sub));
  441. sub->pts = AV_NOPTS_VALUE;
  442. }
  443. #if FF_API_AVCODEC_OPEN
  444. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  445. {
  446. return avcodec_open2(avctx, codec, NULL);
  447. }
  448. #endif
  449. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  450. {
  451. int ret = 0;
  452. AVDictionary *tmp = NULL;
  453. if (options)
  454. av_dict_copy(&tmp, *options, 0);
  455. /* If there is a user-supplied mutex locking routine, call it. */
  456. if (ff_lockmgr_cb) {
  457. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  458. return -1;
  459. }
  460. entangled_thread_counter++;
  461. if(entangled_thread_counter != 1){
  462. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  463. ret = -1;
  464. goto end;
  465. }
  466. if(avctx->codec || !codec) {
  467. ret = AVERROR(EINVAL);
  468. goto end;
  469. }
  470. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  471. if (!avctx->internal) {
  472. ret = AVERROR(ENOMEM);
  473. goto end;
  474. }
  475. if (codec->priv_data_size > 0) {
  476. if(!avctx->priv_data){
  477. avctx->priv_data = av_mallocz(codec->priv_data_size);
  478. if (!avctx->priv_data) {
  479. ret = AVERROR(ENOMEM);
  480. goto end;
  481. }
  482. if (codec->priv_class) {
  483. *(AVClass**)avctx->priv_data= codec->priv_class;
  484. av_opt_set_defaults(avctx->priv_data);
  485. }
  486. }
  487. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  488. goto free_and_end;
  489. } else {
  490. avctx->priv_data = NULL;
  491. }
  492. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  493. goto free_and_end;
  494. if(avctx->coded_width && avctx->coded_height)
  495. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  496. else if(avctx->width && avctx->height)
  497. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  498. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  499. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  500. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  501. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  502. avcodec_set_dimensions(avctx, 0, 0);
  503. }
  504. /* if the decoder init function was already called previously,
  505. free the already allocated subtitle_header before overwriting it */
  506. if (codec->decode)
  507. av_freep(&avctx->subtitle_header);
  508. #define SANE_NB_CHANNELS 128U
  509. if (avctx->channels > SANE_NB_CHANNELS) {
  510. ret = AVERROR(EINVAL);
  511. goto free_and_end;
  512. }
  513. avctx->codec = codec;
  514. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  515. avctx->codec_id == CODEC_ID_NONE) {
  516. avctx->codec_type = codec->type;
  517. avctx->codec_id = codec->id;
  518. }
  519. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  520. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  521. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  522. ret = AVERROR(EINVAL);
  523. goto free_and_end;
  524. }
  525. avctx->frame_number = 0;
  526. #if FF_API_ER
  527. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
  528. avctx->error_recognition, avctx->err_recognition);
  529. switch(avctx->error_recognition){
  530. case FF_ER_EXPLODE : avctx->err_recognition |= AV_EF_EXPLODE | AV_EF_COMPLIANT | AV_EF_CAREFUL;
  531. break;
  532. case FF_ER_VERY_AGGRESSIVE:
  533. case FF_ER_AGGRESSIVE : avctx->err_recognition |= AV_EF_AGGRESSIVE;
  534. case FF_ER_COMPLIANT : avctx->err_recognition |= AV_EF_COMPLIANT;
  535. case FF_ER_CAREFUL : avctx->err_recognition |= AV_EF_CAREFUL;
  536. }
  537. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
  538. avctx->error_recognition, avctx->err_recognition);
  539. #endif
  540. if (!HAVE_THREADS)
  541. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  542. if (HAVE_THREADS && !avctx->thread_opaque) {
  543. ret = ff_thread_init(avctx);
  544. if (ret < 0) {
  545. goto free_and_end;
  546. }
  547. }
  548. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  549. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  550. avctx->codec->max_lowres);
  551. ret = AVERROR(EINVAL);
  552. goto free_and_end;
  553. }
  554. if (avctx->codec->encode) {
  555. int i;
  556. if (avctx->codec->sample_fmts) {
  557. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  558. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  559. break;
  560. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  561. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  562. ret = AVERROR(EINVAL);
  563. goto free_and_end;
  564. }
  565. }
  566. if (avctx->codec->supported_samplerates) {
  567. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  568. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  569. break;
  570. if (avctx->codec->supported_samplerates[i] == 0) {
  571. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  572. ret = AVERROR(EINVAL);
  573. goto free_and_end;
  574. }
  575. }
  576. if (avctx->codec->channel_layouts) {
  577. if (!avctx->channel_layout) {
  578. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  579. } else {
  580. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  581. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  582. break;
  583. if (avctx->codec->channel_layouts[i] == 0) {
  584. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  585. ret = AVERROR(EINVAL);
  586. goto free_and_end;
  587. }
  588. }
  589. }
  590. if (avctx->channel_layout && avctx->channels) {
  591. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  592. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  593. ret = AVERROR(EINVAL);
  594. goto free_and_end;
  595. }
  596. } else if (avctx->channel_layout) {
  597. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  598. }
  599. }
  600. avctx->pts_correction_num_faulty_pts =
  601. avctx->pts_correction_num_faulty_dts = 0;
  602. avctx->pts_correction_last_pts =
  603. avctx->pts_correction_last_dts = INT64_MIN;
  604. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  605. ret = avctx->codec->init(avctx);
  606. if (ret < 0) {
  607. goto free_and_end;
  608. }
  609. }
  610. ret=0;
  611. end:
  612. entangled_thread_counter--;
  613. /* Release any user-supplied mutex. */
  614. if (ff_lockmgr_cb) {
  615. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  616. }
  617. if (options) {
  618. av_dict_free(options);
  619. *options = tmp;
  620. }
  621. return ret;
  622. free_and_end:
  623. av_dict_free(&tmp);
  624. av_freep(&avctx->priv_data);
  625. av_freep(&avctx->internal);
  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. av_freep(&avctx->internal);
  821. if (avctx->codec && avctx->codec->priv_class)
  822. av_opt_free(avctx->priv_data);
  823. av_opt_free(avctx);
  824. av_freep(&avctx->priv_data);
  825. if(avctx->codec && avctx->codec->encode)
  826. av_freep(&avctx->extradata);
  827. avctx->codec = NULL;
  828. avctx->active_thread_type = 0;
  829. entangled_thread_counter--;
  830. /* Release any user-supplied mutex. */
  831. if (ff_lockmgr_cb) {
  832. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  833. }
  834. return 0;
  835. }
  836. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  837. {
  838. switch(id){
  839. case CODEC_ID_G723_1_DEPRECATED : return CODEC_ID_G723_1;
  840. case CODEC_ID_G729_DEPRECATED : return CODEC_ID_G729;
  841. case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  842. default : return id;
  843. }
  844. }
  845. AVCodec *avcodec_find_encoder(enum CodecID id)
  846. {
  847. AVCodec *p, *experimental=NULL;
  848. p = first_avcodec;
  849. id= remap_deprecated_codec_id(id);
  850. while (p) {
  851. if (p->encode != NULL && p->id == id) {
  852. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  853. experimental = p;
  854. } else
  855. return p;
  856. }
  857. p = p->next;
  858. }
  859. return experimental;
  860. }
  861. AVCodec *avcodec_find_encoder_by_name(const char *name)
  862. {
  863. AVCodec *p;
  864. if (!name)
  865. return NULL;
  866. p = first_avcodec;
  867. while (p) {
  868. if (p->encode != NULL && strcmp(name,p->name) == 0)
  869. return p;
  870. p = p->next;
  871. }
  872. return NULL;
  873. }
  874. AVCodec *avcodec_find_decoder(enum CodecID id)
  875. {
  876. AVCodec *p, *experimental=NULL;
  877. p = first_avcodec;
  878. id= remap_deprecated_codec_id(id);
  879. while (p) {
  880. if (p->decode != NULL && p->id == id) {
  881. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  882. experimental = p;
  883. } else
  884. return p;
  885. }
  886. p = p->next;
  887. }
  888. return experimental;
  889. }
  890. AVCodec *avcodec_find_decoder_by_name(const char *name)
  891. {
  892. AVCodec *p;
  893. if (!name)
  894. return NULL;
  895. p = first_avcodec;
  896. while (p) {
  897. if (p->decode != NULL && strcmp(name,p->name) == 0)
  898. return p;
  899. p = p->next;
  900. }
  901. return NULL;
  902. }
  903. static int get_bit_rate(AVCodecContext *ctx)
  904. {
  905. int bit_rate;
  906. int bits_per_sample;
  907. switch(ctx->codec_type) {
  908. case AVMEDIA_TYPE_VIDEO:
  909. case AVMEDIA_TYPE_DATA:
  910. case AVMEDIA_TYPE_SUBTITLE:
  911. case AVMEDIA_TYPE_ATTACHMENT:
  912. bit_rate = ctx->bit_rate;
  913. break;
  914. case AVMEDIA_TYPE_AUDIO:
  915. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  916. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  917. break;
  918. default:
  919. bit_rate = 0;
  920. break;
  921. }
  922. return bit_rate;
  923. }
  924. const char *avcodec_get_name(enum CodecID id)
  925. {
  926. AVCodec *codec;
  927. #if !CONFIG_SMALL
  928. switch (id) {
  929. #include "libavcodec/codec_names.h"
  930. }
  931. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  932. #endif
  933. codec = avcodec_find_decoder(id);
  934. if (codec)
  935. return codec->name;
  936. codec = avcodec_find_encoder(id);
  937. if (codec)
  938. return codec->name;
  939. return "unknown_codec";
  940. }
  941. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  942. {
  943. int i, len, ret = 0;
  944. for (i = 0; i < 4; i++) {
  945. len = snprintf(buf, buf_size,
  946. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  947. buf += len;
  948. buf_size = buf_size > len ? buf_size - len : 0;
  949. ret += len;
  950. codec_tag>>=8;
  951. }
  952. return ret;
  953. }
  954. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  955. {
  956. const char *codec_type;
  957. const char *codec_name;
  958. const char *profile = NULL;
  959. AVCodec *p;
  960. int bitrate;
  961. AVRational display_aspect_ratio;
  962. if (!buf || buf_size <= 0)
  963. return;
  964. codec_type = av_get_media_type_string(enc->codec_type);
  965. codec_name = avcodec_get_name(enc->codec_id);
  966. if (enc->profile != FF_PROFILE_UNKNOWN) {
  967. p = encode ? avcodec_find_encoder(enc->codec_id) :
  968. avcodec_find_decoder(enc->codec_id);
  969. if (p)
  970. profile = av_get_profile_name(p, enc->profile);
  971. }
  972. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  973. codec_name, enc->mb_decision ? " (hq)" : "");
  974. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  975. if (profile)
  976. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  977. if (enc->codec_tag) {
  978. char tag_buf[32];
  979. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  980. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  981. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  982. }
  983. switch(enc->codec_type) {
  984. case AVMEDIA_TYPE_VIDEO:
  985. if (enc->pix_fmt != PIX_FMT_NONE) {
  986. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  987. ", %s",
  988. av_get_pix_fmt_name(enc->pix_fmt));
  989. }
  990. if (enc->width) {
  991. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  992. ", %dx%d",
  993. enc->width, enc->height);
  994. if (enc->sample_aspect_ratio.num) {
  995. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  996. enc->width*enc->sample_aspect_ratio.num,
  997. enc->height*enc->sample_aspect_ratio.den,
  998. 1024*1024);
  999. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1000. " [SAR %d:%d DAR %d:%d]",
  1001. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1002. display_aspect_ratio.num, display_aspect_ratio.den);
  1003. }
  1004. if(av_log_get_level() >= AV_LOG_DEBUG){
  1005. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1006. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1007. ", %d/%d",
  1008. enc->time_base.num/g, enc->time_base.den/g);
  1009. }
  1010. }
  1011. if (encode) {
  1012. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1013. ", q=%d-%d", enc->qmin, enc->qmax);
  1014. }
  1015. break;
  1016. case AVMEDIA_TYPE_AUDIO:
  1017. if (enc->sample_rate) {
  1018. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1019. ", %d Hz", enc->sample_rate);
  1020. }
  1021. av_strlcat(buf, ", ", buf_size);
  1022. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1023. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1024. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1025. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1026. }
  1027. break;
  1028. default:
  1029. return;
  1030. }
  1031. if (encode) {
  1032. if (enc->flags & CODEC_FLAG_PASS1)
  1033. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1034. ", pass 1");
  1035. if (enc->flags & CODEC_FLAG_PASS2)
  1036. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1037. ", pass 2");
  1038. }
  1039. bitrate = get_bit_rate(enc);
  1040. if (bitrate != 0) {
  1041. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1042. ", %d kb/s", bitrate / 1000);
  1043. }
  1044. }
  1045. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1046. {
  1047. const AVProfile *p;
  1048. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1049. return NULL;
  1050. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1051. if (p->profile == profile)
  1052. return p->name;
  1053. return NULL;
  1054. }
  1055. unsigned avcodec_version( void )
  1056. {
  1057. return LIBAVCODEC_VERSION_INT;
  1058. }
  1059. const char *avcodec_configuration(void)
  1060. {
  1061. return FFMPEG_CONFIGURATION;
  1062. }
  1063. const char *avcodec_license(void)
  1064. {
  1065. #define LICENSE_PREFIX "libavcodec license: "
  1066. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1067. }
  1068. void avcodec_flush_buffers(AVCodecContext *avctx)
  1069. {
  1070. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1071. ff_thread_flush(avctx);
  1072. else if(avctx->codec->flush)
  1073. avctx->codec->flush(avctx);
  1074. }
  1075. void avcodec_default_free_buffers(AVCodecContext *s){
  1076. AVCodecInternal *avci = s->internal;
  1077. int i, j;
  1078. if (!avci->buffer)
  1079. return;
  1080. if (avci->buffer_count)
  1081. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1082. avci->buffer_count);
  1083. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1084. InternalBuffer *buf = &avci->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(&avci->buffer);
  1091. avci->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. }