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.

1450 lines
43KB

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