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.

1189 lines
34KB

  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/integer.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavcore/audioconvert.h"
  31. #include "libavcore/imgutils.h"
  32. #include "libavcore/internal.h"
  33. #include "libavcore/samplefmt.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #include "libavutil/opt.h"
  37. #include "imgconvert.h"
  38. #include "audioconvert.h"
  39. #include "internal.h"
  40. #include <stdlib.h>
  41. #include <stdarg.h>
  42. #include <limits.h>
  43. #include <float.h>
  44. static int volatile entangled_thread_counter=0;
  45. int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  46. static void *codec_mutex;
  47. void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
  48. {
  49. if(min_size < *size)
  50. return ptr;
  51. *size= FFMAX(17*min_size/16 + 32, min_size);
  52. ptr= av_realloc(ptr, *size);
  53. 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
  54. *size= 0;
  55. return ptr;
  56. }
  57. void av_fast_malloc(void *ptr, unsigned int *size, unsigned int min_size)
  58. {
  59. void **p = ptr;
  60. if (min_size < *size)
  61. return;
  62. *size= FFMAX(17*min_size/16 + 32, min_size);
  63. av_free(*p);
  64. *p = av_malloc(*size);
  65. if (!*p) *size = 0;
  66. }
  67. /* encoder management */
  68. static AVCodec *first_avcodec = NULL;
  69. AVCodec *av_codec_next(AVCodec *c){
  70. if(c) return c->next;
  71. else return first_avcodec;
  72. }
  73. void avcodec_register(AVCodec *codec)
  74. {
  75. AVCodec **p;
  76. avcodec_init();
  77. p = &first_avcodec;
  78. while (*p != NULL) p = &(*p)->next;
  79. *p = codec;
  80. codec->next = NULL;
  81. }
  82. #if LIBAVCODEC_VERSION_MAJOR < 53
  83. void register_avcodec(AVCodec *codec)
  84. {
  85. avcodec_register(codec);
  86. }
  87. #endif
  88. unsigned avcodec_get_edge_width(void)
  89. {
  90. return EDGE_WIDTH;
  91. }
  92. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  93. s->coded_width = width;
  94. s->coded_height= height;
  95. s->width = -((-width )>>s->lowres);
  96. s->height= -((-height)>>s->lowres);
  97. }
  98. typedef struct InternalBuffer{
  99. int last_pic_num;
  100. uint8_t *base[4];
  101. uint8_t *data[4];
  102. int linesize[4];
  103. int width, height;
  104. enum PixelFormat pix_fmt;
  105. }InternalBuffer;
  106. #define INTERNAL_BUFFER_SIZE 32
  107. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  108. int w_align= 1;
  109. int h_align= 1;
  110. switch(s->pix_fmt){
  111. case PIX_FMT_YUV420P:
  112. case PIX_FMT_YUYV422:
  113. case PIX_FMT_UYVY422:
  114. case PIX_FMT_YUV422P:
  115. case PIX_FMT_YUV440P:
  116. case PIX_FMT_YUV444P:
  117. case PIX_FMT_GRAY8:
  118. case PIX_FMT_GRAY16BE:
  119. case PIX_FMT_GRAY16LE:
  120. case PIX_FMT_YUVJ420P:
  121. case PIX_FMT_YUVJ422P:
  122. case PIX_FMT_YUVJ440P:
  123. case PIX_FMT_YUVJ444P:
  124. case PIX_FMT_YUVA420P:
  125. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  126. h_align= 16;
  127. 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)
  128. h_align= 32; // interlaced is rounded up to 2 MBs
  129. break;
  130. case PIX_FMT_YUV411P:
  131. case PIX_FMT_UYYVYY411:
  132. w_align=32;
  133. h_align=8;
  134. break;
  135. case PIX_FMT_YUV410P:
  136. if(s->codec_id == CODEC_ID_SVQ1){
  137. w_align=64;
  138. h_align=64;
  139. }
  140. case PIX_FMT_RGB555:
  141. if(s->codec_id == CODEC_ID_RPZA){
  142. w_align=4;
  143. h_align=4;
  144. }
  145. case PIX_FMT_PAL8:
  146. case PIX_FMT_BGR8:
  147. case PIX_FMT_RGB8:
  148. if(s->codec_id == CODEC_ID_SMC){
  149. w_align=4;
  150. h_align=4;
  151. }
  152. break;
  153. case PIX_FMT_BGR24:
  154. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  155. w_align=4;
  156. h_align=4;
  157. }
  158. break;
  159. default:
  160. w_align= 1;
  161. h_align= 1;
  162. break;
  163. }
  164. *width = FFALIGN(*width , w_align);
  165. *height= FFALIGN(*height, h_align);
  166. if(s->codec_id == CODEC_ID_H264)
  167. *height+=2; // some of the optimized chroma MC reads one line too much
  168. linesize_align[0] =
  169. linesize_align[1] =
  170. linesize_align[2] =
  171. linesize_align[3] = STRIDE_ALIGN;
  172. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  173. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  174. //picture size unneccessarily in some cases. The solution here is not
  175. //pretty and better ideas are welcome!
  176. #if HAVE_MMX
  177. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  178. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  179. s->codec_id == CODEC_ID_VP6A) {
  180. linesize_align[0] =
  181. linesize_align[1] =
  182. linesize_align[2] = 16;
  183. }
  184. #endif
  185. }
  186. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  187. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  188. int linesize_align[4];
  189. int align;
  190. avcodec_align_dimensions2(s, width, height, linesize_align);
  191. align = FFMAX(linesize_align[0], linesize_align[3]);
  192. linesize_align[1] <<= chroma_shift;
  193. linesize_align[2] <<= chroma_shift;
  194. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  195. *width=FFALIGN(*width, align);
  196. }
  197. #if LIBAVCODEC_VERSION_MAJOR < 53
  198. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  199. return av_image_check_size(w, h, 0, av_log_ctx);
  200. }
  201. #endif
  202. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  203. int i;
  204. int w= s->width;
  205. int h= s->height;
  206. InternalBuffer *buf;
  207. int *picture_number;
  208. if(pic->data[0]!=NULL) {
  209. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  210. return -1;
  211. }
  212. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  213. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  214. return -1;
  215. }
  216. if(av_image_check_size(w, h, 0, s))
  217. return -1;
  218. if(s->internal_buffer==NULL){
  219. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  220. }
  221. #if 0
  222. s->internal_buffer= av_fast_realloc(
  223. s->internal_buffer,
  224. &s->internal_buffer_size,
  225. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  226. );
  227. #endif
  228. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  229. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  230. (*picture_number)++;
  231. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  232. for(i=0; i<4; i++){
  233. av_freep(&buf->base[i]);
  234. buf->data[i]= NULL;
  235. }
  236. }
  237. if(buf->base[0]){
  238. pic->age= *picture_number - buf->last_pic_num;
  239. buf->last_pic_num= *picture_number;
  240. }else{
  241. int h_chroma_shift, v_chroma_shift;
  242. int size[4] = {0};
  243. int tmpsize;
  244. int unaligned;
  245. AVPicture picture;
  246. int stride_align[4];
  247. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  248. avcodec_align_dimensions2(s, &w, &h, stride_align);
  249. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  250. w+= EDGE_WIDTH*2;
  251. h+= EDGE_WIDTH*2;
  252. }
  253. do {
  254. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  255. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  256. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  257. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  258. w += w & ~(w-1);
  259. unaligned = 0;
  260. for (i=0; i<4; i++){
  261. unaligned |= picture.linesize[i] % stride_align[i];
  262. }
  263. } while (unaligned);
  264. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  265. if (tmpsize < 0)
  266. return -1;
  267. for (i=0; i<3 && picture.data[i+1]; i++)
  268. size[i] = picture.data[i+1] - picture.data[i];
  269. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  270. buf->last_pic_num= -256*256*256*64;
  271. memset(buf->base, 0, sizeof(buf->base));
  272. memset(buf->data, 0, sizeof(buf->data));
  273. for(i=0; i<4 && size[i]; i++){
  274. const int h_shift= i==0 ? 0 : h_chroma_shift;
  275. const int v_shift= i==0 ? 0 : v_chroma_shift;
  276. buf->linesize[i]= picture.linesize[i];
  277. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  278. if(buf->base[i]==NULL) return -1;
  279. memset(buf->base[i], 128, size[i]);
  280. // no edge if EDGE EMU or not planar YUV
  281. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  282. buf->data[i] = buf->base[i];
  283. else
  284. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
  285. }
  286. if(size[1] && !size[2])
  287. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  288. buf->width = s->width;
  289. buf->height = s->height;
  290. buf->pix_fmt= s->pix_fmt;
  291. pic->age= 256*256*256*64;
  292. }
  293. pic->type= FF_BUFFER_TYPE_INTERNAL;
  294. for(i=0; i<4; i++){
  295. pic->base[i]= buf->base[i];
  296. pic->data[i]= buf->data[i];
  297. pic->linesize[i]= buf->linesize[i];
  298. }
  299. s->internal_buffer_count++;
  300. pic->reordered_opaque= s->reordered_opaque;
  301. if(s->debug&FF_DEBUG_BUFFERS)
  302. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  303. return 0;
  304. }
  305. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  306. int i;
  307. InternalBuffer *buf, *last;
  308. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  309. assert(s->internal_buffer_count);
  310. buf = NULL; /* avoids warning */
  311. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  312. buf= &((InternalBuffer*)s->internal_buffer)[i];
  313. if(buf->data[0] == pic->data[0])
  314. break;
  315. }
  316. assert(i < s->internal_buffer_count);
  317. s->internal_buffer_count--;
  318. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  319. FFSWAP(InternalBuffer, *buf, *last);
  320. for(i=0; i<4; i++){
  321. pic->data[i]=NULL;
  322. // pic->base[i]=NULL;
  323. }
  324. //printf("R%X\n", pic->opaque);
  325. if(s->debug&FF_DEBUG_BUFFERS)
  326. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  327. }
  328. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  329. AVFrame temp_pic;
  330. int i;
  331. /* If no picture return a new buffer */
  332. if(pic->data[0] == NULL) {
  333. /* We will copy from buffer, so must be readable */
  334. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  335. return s->get_buffer(s, pic);
  336. }
  337. /* If internal buffer type return the same buffer */
  338. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  339. pic->reordered_opaque= s->reordered_opaque;
  340. return 0;
  341. }
  342. /*
  343. * Not internal type and reget_buffer not overridden, emulate cr buffer
  344. */
  345. temp_pic = *pic;
  346. for(i = 0; i < 4; i++)
  347. pic->data[i] = pic->base[i] = NULL;
  348. pic->opaque = NULL;
  349. /* Allocate new frame */
  350. if (s->get_buffer(s, pic))
  351. return -1;
  352. /* Copy image data from old buffer to new buffer */
  353. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  354. s->height);
  355. s->release_buffer(s, &temp_pic); // Release old frame
  356. return 0;
  357. }
  358. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  359. int i;
  360. for(i=0; i<count; i++){
  361. int r= func(c, (char*)arg + i*size);
  362. if(ret) ret[i]= r;
  363. }
  364. return 0;
  365. }
  366. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  367. int i;
  368. for(i=0; i<count; i++){
  369. int r= func(c, arg, i, 0);
  370. if(ret) ret[i]= r;
  371. }
  372. return 0;
  373. }
  374. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  375. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  376. ++fmt;
  377. return fmt[0];
  378. }
  379. void avcodec_get_frame_defaults(AVFrame *pic){
  380. memset(pic, 0, sizeof(AVFrame));
  381. pic->pts= AV_NOPTS_VALUE;
  382. pic->key_frame= 1;
  383. }
  384. AVFrame *avcodec_alloc_frame(void){
  385. AVFrame *pic= av_malloc(sizeof(AVFrame));
  386. if(pic==NULL) return NULL;
  387. avcodec_get_frame_defaults(pic);
  388. return pic;
  389. }
  390. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  391. {
  392. int ret= -1;
  393. /* If there is a user-supplied mutex locking routine, call it. */
  394. if (ff_lockmgr_cb) {
  395. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  396. return -1;
  397. }
  398. entangled_thread_counter++;
  399. if(entangled_thread_counter != 1){
  400. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  401. goto end;
  402. }
  403. if(avctx->codec || !codec)
  404. goto end;
  405. if (codec->priv_data_size > 0) {
  406. if(!avctx->priv_data){
  407. avctx->priv_data = av_mallocz(codec->priv_data_size);
  408. if (!avctx->priv_data) {
  409. ret = AVERROR(ENOMEM);
  410. goto end;
  411. }
  412. if(codec->priv_class){ //this can be droped once all user apps use avcodec_get_context_defaults3()
  413. *(AVClass**)avctx->priv_data= codec->priv_class;
  414. av_opt_set_defaults(avctx->priv_data);
  415. }
  416. }
  417. } else {
  418. avctx->priv_data = NULL;
  419. }
  420. if(avctx->coded_width && avctx->coded_height)
  421. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  422. else if(avctx->width && avctx->height)
  423. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  424. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  425. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  426. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  427. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  428. avcodec_set_dimensions(avctx, 0, 0);
  429. }
  430. #define SANE_NB_CHANNELS 128U
  431. if (avctx->channels > SANE_NB_CHANNELS) {
  432. ret = AVERROR(EINVAL);
  433. goto free_and_end;
  434. }
  435. avctx->codec = codec;
  436. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  437. avctx->codec_id == CODEC_ID_NONE) {
  438. avctx->codec_type = codec->type;
  439. avctx->codec_id = codec->id;
  440. }
  441. if(avctx->codec_id != codec->id || avctx->codec_type != codec->type){
  442. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  443. goto free_and_end;
  444. }
  445. avctx->frame_number = 0;
  446. if (avctx->codec->max_lowres < avctx->lowres) {
  447. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  448. avctx->codec->max_lowres);
  449. goto free_and_end;
  450. }
  451. if(avctx->codec->init){
  452. ret = avctx->codec->init(avctx);
  453. if (ret < 0) {
  454. goto free_and_end;
  455. }
  456. }
  457. ret=0;
  458. end:
  459. entangled_thread_counter--;
  460. /* Release any user-supplied mutex. */
  461. if (ff_lockmgr_cb) {
  462. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  463. }
  464. return ret;
  465. free_and_end:
  466. av_freep(&avctx->priv_data);
  467. avctx->codec= NULL;
  468. goto end;
  469. }
  470. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  471. const short *samples)
  472. {
  473. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  474. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  475. return -1;
  476. }
  477. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  478. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  479. avctx->frame_number++;
  480. return ret;
  481. }else
  482. return 0;
  483. }
  484. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  485. const AVFrame *pict)
  486. {
  487. if(buf_size < FF_MIN_BUFFER_SIZE){
  488. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  489. return -1;
  490. }
  491. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  492. return -1;
  493. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  494. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  495. avctx->frame_number++;
  496. emms_c(); //needed to avoid an emms_c() call before every return;
  497. return ret;
  498. }else
  499. return 0;
  500. }
  501. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  502. const AVSubtitle *sub)
  503. {
  504. int ret;
  505. if(sub->start_display_time) {
  506. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  507. return -1;
  508. }
  509. if(sub->num_rects == 0 || !sub->rects)
  510. return -1;
  511. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  512. avctx->frame_number++;
  513. return ret;
  514. }
  515. #if FF_API_VIDEO_OLD
  516. int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  517. int *got_picture_ptr,
  518. const uint8_t *buf, int buf_size)
  519. {
  520. AVPacket avpkt;
  521. av_init_packet(&avpkt);
  522. avpkt.data = buf;
  523. avpkt.size = buf_size;
  524. // HACK for CorePNG to decode as normal PNG by default
  525. avpkt.flags = AV_PKT_FLAG_KEY;
  526. return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
  527. }
  528. #endif
  529. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  530. int *got_picture_ptr,
  531. AVPacket *avpkt)
  532. {
  533. int ret;
  534. *got_picture_ptr= 0;
  535. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  536. return -1;
  537. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  538. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  539. avpkt);
  540. emms_c(); //needed to avoid an emms_c() call before every return;
  541. if (*got_picture_ptr)
  542. avctx->frame_number++;
  543. }else
  544. ret= 0;
  545. return ret;
  546. }
  547. #if FF_API_AUDIO_OLD
  548. int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
  549. int *frame_size_ptr,
  550. const uint8_t *buf, int buf_size)
  551. {
  552. AVPacket avpkt;
  553. av_init_packet(&avpkt);
  554. avpkt.data = buf;
  555. avpkt.size = buf_size;
  556. return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
  557. }
  558. #endif
  559. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  560. int *frame_size_ptr,
  561. AVPacket *avpkt)
  562. {
  563. int ret;
  564. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  565. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  566. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  567. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  568. return -1;
  569. }
  570. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  571. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  572. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  573. return -1;
  574. }
  575. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  576. avctx->frame_number++;
  577. }else{
  578. ret= 0;
  579. *frame_size_ptr=0;
  580. }
  581. return ret;
  582. }
  583. #if FF_API_SUBTITLE_OLD
  584. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  585. int *got_sub_ptr,
  586. const uint8_t *buf, int buf_size)
  587. {
  588. AVPacket avpkt;
  589. av_init_packet(&avpkt);
  590. avpkt.data = buf;
  591. avpkt.size = buf_size;
  592. return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
  593. }
  594. #endif
  595. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  596. int *got_sub_ptr,
  597. AVPacket *avpkt)
  598. {
  599. int ret;
  600. *got_sub_ptr = 0;
  601. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  602. if (*got_sub_ptr)
  603. avctx->frame_number++;
  604. return ret;
  605. }
  606. void avsubtitle_free(AVSubtitle *sub)
  607. {
  608. int i;
  609. for (i = 0; i < sub->num_rects; i++)
  610. {
  611. av_freep(&sub->rects[i]->pict.data[0]);
  612. av_freep(&sub->rects[i]->pict.data[1]);
  613. av_freep(&sub->rects[i]->pict.data[2]);
  614. av_freep(&sub->rects[i]->pict.data[3]);
  615. av_freep(&sub->rects[i]->text);
  616. av_freep(&sub->rects[i]->ass);
  617. av_freep(&sub->rects[i]);
  618. }
  619. av_freep(&sub->rects);
  620. memset(sub, 0, sizeof(AVSubtitle));
  621. }
  622. av_cold int avcodec_close(AVCodecContext *avctx)
  623. {
  624. /* If there is a user-supplied mutex locking routine, call it. */
  625. if (ff_lockmgr_cb) {
  626. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  627. return -1;
  628. }
  629. entangled_thread_counter++;
  630. if(entangled_thread_counter != 1){
  631. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  632. entangled_thread_counter--;
  633. return -1;
  634. }
  635. if (HAVE_THREADS && avctx->thread_opaque)
  636. avcodec_thread_free(avctx);
  637. if (avctx->codec && avctx->codec->close)
  638. avctx->codec->close(avctx);
  639. avcodec_default_free_buffers(avctx);
  640. avctx->coded_frame = NULL;
  641. av_freep(&avctx->priv_data);
  642. if(avctx->codec && avctx->codec->encode)
  643. av_freep(&avctx->extradata);
  644. avctx->codec = NULL;
  645. entangled_thread_counter--;
  646. /* Release any user-supplied mutex. */
  647. if (ff_lockmgr_cb) {
  648. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  649. }
  650. return 0;
  651. }
  652. AVCodec *avcodec_find_encoder(enum CodecID id)
  653. {
  654. AVCodec *p, *experimental=NULL;
  655. p = first_avcodec;
  656. while (p) {
  657. if (p->encode != NULL && p->id == id) {
  658. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  659. experimental = p;
  660. } else
  661. return p;
  662. }
  663. p = p->next;
  664. }
  665. return experimental;
  666. }
  667. AVCodec *avcodec_find_encoder_by_name(const char *name)
  668. {
  669. AVCodec *p;
  670. if (!name)
  671. return NULL;
  672. p = first_avcodec;
  673. while (p) {
  674. if (p->encode != NULL && strcmp(name,p->name) == 0)
  675. return p;
  676. p = p->next;
  677. }
  678. return NULL;
  679. }
  680. AVCodec *avcodec_find_decoder(enum CodecID id)
  681. {
  682. AVCodec *p;
  683. p = first_avcodec;
  684. while (p) {
  685. if (p->decode != NULL && p->id == id)
  686. return p;
  687. p = p->next;
  688. }
  689. return NULL;
  690. }
  691. AVCodec *avcodec_find_decoder_by_name(const char *name)
  692. {
  693. AVCodec *p;
  694. if (!name)
  695. return NULL;
  696. p = first_avcodec;
  697. while (p) {
  698. if (p->decode != NULL && strcmp(name,p->name) == 0)
  699. return p;
  700. p = p->next;
  701. }
  702. return NULL;
  703. }
  704. static int get_bit_rate(AVCodecContext *ctx)
  705. {
  706. int bit_rate;
  707. int bits_per_sample;
  708. switch(ctx->codec_type) {
  709. case AVMEDIA_TYPE_VIDEO:
  710. case AVMEDIA_TYPE_DATA:
  711. case AVMEDIA_TYPE_SUBTITLE:
  712. case AVMEDIA_TYPE_ATTACHMENT:
  713. bit_rate = ctx->bit_rate;
  714. break;
  715. case AVMEDIA_TYPE_AUDIO:
  716. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  717. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  718. break;
  719. default:
  720. bit_rate = 0;
  721. break;
  722. }
  723. return bit_rate;
  724. }
  725. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  726. {
  727. int i, len, ret = 0;
  728. for (i = 0; i < 4; i++) {
  729. len = snprintf(buf, buf_size,
  730. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  731. buf += len;
  732. buf_size = buf_size > len ? buf_size - len : 0;
  733. ret += len;
  734. codec_tag>>=8;
  735. }
  736. return ret;
  737. }
  738. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  739. {
  740. const char *codec_name;
  741. AVCodec *p;
  742. char buf1[32];
  743. int bitrate;
  744. AVRational display_aspect_ratio;
  745. if (encode)
  746. p = avcodec_find_encoder(enc->codec_id);
  747. else
  748. p = avcodec_find_decoder(enc->codec_id);
  749. if (p) {
  750. codec_name = p->name;
  751. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  752. /* fake mpeg2 transport stream codec (currently not
  753. registered) */
  754. codec_name = "mpeg2ts";
  755. } else if (enc->codec_name[0] != '\0') {
  756. codec_name = enc->codec_name;
  757. } else {
  758. /* output avi tags */
  759. char tag_buf[32];
  760. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  761. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  762. codec_name = buf1;
  763. }
  764. switch(enc->codec_type) {
  765. case AVMEDIA_TYPE_VIDEO:
  766. snprintf(buf, buf_size,
  767. "Video: %s%s",
  768. codec_name, enc->mb_decision ? " (hq)" : "");
  769. if (enc->pix_fmt != PIX_FMT_NONE) {
  770. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  771. ", %s",
  772. avcodec_get_pix_fmt_name(enc->pix_fmt));
  773. }
  774. if (enc->width) {
  775. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  776. ", %dx%d",
  777. enc->width, enc->height);
  778. if (enc->sample_aspect_ratio.num) {
  779. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  780. enc->width*enc->sample_aspect_ratio.num,
  781. enc->height*enc->sample_aspect_ratio.den,
  782. 1024*1024);
  783. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  784. " [PAR %d:%d DAR %d:%d]",
  785. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  786. display_aspect_ratio.num, display_aspect_ratio.den);
  787. }
  788. if(av_log_get_level() >= AV_LOG_DEBUG){
  789. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  790. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  791. ", %d/%d",
  792. enc->time_base.num/g, enc->time_base.den/g);
  793. }
  794. }
  795. if (encode) {
  796. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  797. ", q=%d-%d", enc->qmin, enc->qmax);
  798. }
  799. break;
  800. case AVMEDIA_TYPE_AUDIO:
  801. snprintf(buf, buf_size,
  802. "Audio: %s",
  803. codec_name);
  804. if (enc->sample_rate) {
  805. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  806. ", %d Hz", enc->sample_rate);
  807. }
  808. av_strlcat(buf, ", ", buf_size);
  809. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  810. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  811. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  812. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  813. }
  814. break;
  815. case AVMEDIA_TYPE_DATA:
  816. snprintf(buf, buf_size, "Data: %s", codec_name);
  817. break;
  818. case AVMEDIA_TYPE_SUBTITLE:
  819. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  820. break;
  821. case AVMEDIA_TYPE_ATTACHMENT:
  822. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  823. break;
  824. default:
  825. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  826. return;
  827. }
  828. if (encode) {
  829. if (enc->flags & CODEC_FLAG_PASS1)
  830. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  831. ", pass 1");
  832. if (enc->flags & CODEC_FLAG_PASS2)
  833. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  834. ", pass 2");
  835. }
  836. bitrate = get_bit_rate(enc);
  837. if (bitrate != 0) {
  838. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  839. ", %d kb/s", bitrate / 1000);
  840. }
  841. }
  842. unsigned avcodec_version( void )
  843. {
  844. return LIBAVCODEC_VERSION_INT;
  845. }
  846. const char *avcodec_configuration(void)
  847. {
  848. return FFMPEG_CONFIGURATION;
  849. }
  850. const char *avcodec_license(void)
  851. {
  852. #define LICENSE_PREFIX "libavcodec license: "
  853. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  854. }
  855. void avcodec_init(void)
  856. {
  857. static int initialized = 0;
  858. if (initialized != 0)
  859. return;
  860. initialized = 1;
  861. dsputil_static_init();
  862. }
  863. void avcodec_flush_buffers(AVCodecContext *avctx)
  864. {
  865. if(avctx->codec->flush)
  866. avctx->codec->flush(avctx);
  867. }
  868. void avcodec_default_free_buffers(AVCodecContext *s){
  869. int i, j;
  870. if(s->internal_buffer==NULL) return;
  871. if (s->internal_buffer_count)
  872. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  873. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  874. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  875. for(j=0; j<4; j++){
  876. av_freep(&buf->base[j]);
  877. buf->data[j]= NULL;
  878. }
  879. }
  880. av_freep(&s->internal_buffer);
  881. s->internal_buffer_count=0;
  882. }
  883. char av_get_pict_type_char(int pict_type){
  884. switch(pict_type){
  885. case FF_I_TYPE: return 'I';
  886. case FF_P_TYPE: return 'P';
  887. case FF_B_TYPE: return 'B';
  888. case FF_S_TYPE: return 'S';
  889. case FF_SI_TYPE:return 'i';
  890. case FF_SP_TYPE:return 'p';
  891. case FF_BI_TYPE:return 'b';
  892. default: return '?';
  893. }
  894. }
  895. int av_get_bits_per_sample(enum CodecID codec_id){
  896. switch(codec_id){
  897. case CODEC_ID_ADPCM_SBPRO_2:
  898. return 2;
  899. case CODEC_ID_ADPCM_SBPRO_3:
  900. return 3;
  901. case CODEC_ID_ADPCM_SBPRO_4:
  902. case CODEC_ID_ADPCM_CT:
  903. case CODEC_ID_ADPCM_IMA_WAV:
  904. case CODEC_ID_ADPCM_MS:
  905. case CODEC_ID_ADPCM_YAMAHA:
  906. return 4;
  907. case CODEC_ID_PCM_ALAW:
  908. case CODEC_ID_PCM_MULAW:
  909. case CODEC_ID_PCM_S8:
  910. case CODEC_ID_PCM_U8:
  911. case CODEC_ID_PCM_ZORK:
  912. return 8;
  913. case CODEC_ID_PCM_S16BE:
  914. case CODEC_ID_PCM_S16LE:
  915. case CODEC_ID_PCM_S16LE_PLANAR:
  916. case CODEC_ID_PCM_U16BE:
  917. case CODEC_ID_PCM_U16LE:
  918. return 16;
  919. case CODEC_ID_PCM_S24DAUD:
  920. case CODEC_ID_PCM_S24BE:
  921. case CODEC_ID_PCM_S24LE:
  922. case CODEC_ID_PCM_U24BE:
  923. case CODEC_ID_PCM_U24LE:
  924. return 24;
  925. case CODEC_ID_PCM_S32BE:
  926. case CODEC_ID_PCM_S32LE:
  927. case CODEC_ID_PCM_U32BE:
  928. case CODEC_ID_PCM_U32LE:
  929. case CODEC_ID_PCM_F32BE:
  930. case CODEC_ID_PCM_F32LE:
  931. return 32;
  932. case CODEC_ID_PCM_F64BE:
  933. case CODEC_ID_PCM_F64LE:
  934. return 64;
  935. default:
  936. return 0;
  937. }
  938. }
  939. #if FF_API_OLD_SAMPLE_FMT
  940. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  941. return av_get_bits_per_sample_fmt(sample_fmt);
  942. }
  943. #endif
  944. #if !HAVE_THREADS
  945. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  946. s->thread_count = thread_count;
  947. return -1;
  948. }
  949. #endif
  950. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  951. {
  952. unsigned int n = 0;
  953. while(v >= 0xff) {
  954. *s++ = 0xff;
  955. v -= 0xff;
  956. n++;
  957. }
  958. *s = v;
  959. n++;
  960. return n;
  961. }
  962. #if LIBAVCODEC_VERSION_MAJOR < 53
  963. #include "libavcore/parseutils.h"
  964. int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
  965. {
  966. return av_parse_video_size(width_ptr, height_ptr, str);
  967. }
  968. int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
  969. {
  970. return av_parse_video_rate(frame_rate, arg);
  971. }
  972. #endif
  973. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  974. int i;
  975. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  976. return i;
  977. }
  978. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  979. {
  980. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  981. "version to the newest one from SVN. If the problem still "
  982. "occurs, it means that your file has a feature which has not "
  983. "been implemented.", feature);
  984. if(want_sample)
  985. av_log_ask_for_sample(avc, NULL);
  986. else
  987. av_log(avc, AV_LOG_WARNING, "\n");
  988. }
  989. void av_log_ask_for_sample(void *avc, const char *msg)
  990. {
  991. if (msg)
  992. av_log(avc, AV_LOG_WARNING, "%s ", msg);
  993. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  994. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  995. "and contact the ffmpeg-devel mailing list.\n");
  996. }
  997. static AVHWAccel *first_hwaccel = NULL;
  998. void av_register_hwaccel(AVHWAccel *hwaccel)
  999. {
  1000. AVHWAccel **p = &first_hwaccel;
  1001. while (*p)
  1002. p = &(*p)->next;
  1003. *p = hwaccel;
  1004. hwaccel->next = NULL;
  1005. }
  1006. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1007. {
  1008. return hwaccel ? hwaccel->next : first_hwaccel;
  1009. }
  1010. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1011. {
  1012. AVHWAccel *hwaccel=NULL;
  1013. while((hwaccel= av_hwaccel_next(hwaccel))){
  1014. if ( hwaccel->id == codec_id
  1015. && hwaccel->pix_fmt == pix_fmt)
  1016. return hwaccel;
  1017. }
  1018. return NULL;
  1019. }
  1020. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1021. {
  1022. if (ff_lockmgr_cb) {
  1023. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1024. return -1;
  1025. }
  1026. ff_lockmgr_cb = cb;
  1027. if (ff_lockmgr_cb) {
  1028. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1029. return -1;
  1030. }
  1031. return 0;
  1032. }
  1033. unsigned int ff_toupper4(unsigned int x)
  1034. {
  1035. return toupper( x &0xFF)
  1036. + (toupper((x>>8 )&0xFF)<<8 )
  1037. + (toupper((x>>16)&0xFF)<<16)
  1038. + (toupper((x>>24)&0xFF)<<24);
  1039. }