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.

584 lines
15KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #include "mpegvideo.h"
  22. void *av_mallocz(unsigned int size)
  23. {
  24. void *ptr;
  25. if(size == 0) fprintf(stderr, "Warning, allocating 0 bytes\n");
  26. ptr = av_malloc(size);
  27. if (!ptr)
  28. return NULL;
  29. memset(ptr, 0, size);
  30. return ptr;
  31. }
  32. /* allocation of static arrays - do not use for normal allocation */
  33. static unsigned int last_static = 0;
  34. static char*** array_static = NULL;
  35. static const unsigned int grow_static = 64; // ^2
  36. void *__av_mallocz_static(void** location, unsigned int size)
  37. {
  38. int l = (last_static + grow_static) & ~(grow_static - 1);
  39. void *ptr = av_mallocz(size);
  40. if (!ptr)
  41. return NULL;
  42. if (location)
  43. {
  44. if (l > last_static)
  45. array_static = realloc(array_static, l);
  46. array_static[last_static++] = (char**) location;
  47. *location = ptr;
  48. }
  49. return ptr;
  50. }
  51. /* free all static arrays and reset pointers to 0 */
  52. void av_free_static()
  53. {
  54. if (array_static)
  55. {
  56. unsigned i;
  57. for (i = 0; i < last_static; i++)
  58. {
  59. free(*array_static[i]);
  60. *array_static[i] = NULL;
  61. }
  62. free(array_static);
  63. array_static = 0;
  64. }
  65. last_static = 0;
  66. }
  67. /* cannot call it directly because of 'void **' casting is not automatic */
  68. void __av_freep(void **ptr)
  69. {
  70. av_free(*ptr);
  71. *ptr = NULL;
  72. }
  73. /* encoder management */
  74. AVCodec *first_avcodec;
  75. void register_avcodec(AVCodec *format)
  76. {
  77. AVCodec **p;
  78. p = &first_avcodec;
  79. while (*p != NULL) p = &(*p)->next;
  80. *p = format;
  81. format->next = NULL;
  82. }
  83. typedef struct DefaultPicOpaque{
  84. int last_pic_num;
  85. uint8_t *data[4];
  86. }DefaultPicOpaque;
  87. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  88. int i;
  89. const int width = s->width;
  90. const int height= s->height;
  91. DefaultPicOpaque *opaque;
  92. assert(pic->data[0]==NULL);
  93. assert(pic->type==0 || pic->type==FF_TYPE_INTERNAL);
  94. if(pic->opaque){
  95. opaque= (DefaultPicOpaque *)pic->opaque;
  96. for(i=0; i<3; i++)
  97. pic->data[i]= opaque->data[i];
  98. // printf("get_buffer %X coded_pic_num:%d last:%d\n", pic->opaque, pic->coded_picture_number, opaque->last_pic_num);
  99. pic->age= pic->coded_picture_number - opaque->last_pic_num;
  100. opaque->last_pic_num= pic->coded_picture_number;
  101. //printf("age: %d %d %d\n", pic->age, c->picture_number, pic->coded_picture_number);
  102. }else{
  103. int align, h_chroma_shift, v_chroma_shift;
  104. int w, h, pixel_size;
  105. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  106. switch(s->pix_fmt){
  107. case PIX_FMT_YUV422:
  108. pixel_size=2;
  109. break;
  110. case PIX_FMT_RGB24:
  111. case PIX_FMT_BGR24:
  112. pixel_size=3;
  113. break;
  114. case PIX_FMT_RGBA32:
  115. pixel_size=4;
  116. break;
  117. default:
  118. pixel_size=1;
  119. }
  120. if(s->codec_id==CODEC_ID_SVQ1) align=63;
  121. else align=15;
  122. w= (width +align)&~align;
  123. h= (height+align)&~align;
  124. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  125. w+= EDGE_WIDTH*2;
  126. h+= EDGE_WIDTH*2;
  127. }
  128. opaque= av_mallocz(sizeof(DefaultPicOpaque));
  129. if(opaque==NULL) return -1;
  130. pic->opaque= opaque;
  131. opaque->last_pic_num= -256*256*256*64;
  132. for(i=0; i<3; i++){
  133. int h_shift= i==0 ? 0 : h_chroma_shift;
  134. int v_shift= i==0 ? 0 : v_chroma_shift;
  135. pic->linesize[i]= pixel_size*w>>h_shift;
  136. pic->base[i]= av_mallocz((pic->linesize[i]*h>>v_shift)+16); //FIXME 16
  137. if(pic->base[i]==NULL) return -1;
  138. memset(pic->base[i], 128, pic->linesize[i]*h>>v_shift);
  139. if(s->flags&CODEC_FLAG_EMU_EDGE)
  140. pic->data[i] = pic->base[i] + 16; //FIXME 16
  141. else
  142. pic->data[i] = pic->base[i] + (pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift) + 16; //FIXME 16
  143. opaque->data[i]= pic->data[i];
  144. }
  145. pic->age= 256*256*256*64;
  146. pic->type= FF_BUFFER_TYPE_INTERNAL;
  147. }
  148. return 0;
  149. }
  150. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  151. int i;
  152. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  153. for(i=0; i<3; i++)
  154. pic->data[i]=NULL;
  155. //printf("R%X\n", pic->opaque);
  156. }
  157. void avcodec_get_context_defaults(AVCodecContext *s){
  158. s->bit_rate= 800*1000;
  159. s->bit_rate_tolerance= s->bit_rate*10;
  160. s->qmin= 2;
  161. s->qmax= 31;
  162. s->mb_qmin= 2;
  163. s->mb_qmax= 31;
  164. s->rc_eq= "tex^qComp";
  165. s->qcompress= 0.5;
  166. s->max_qdiff= 3;
  167. s->b_quant_factor=1.25;
  168. s->b_quant_offset=1.25;
  169. s->i_quant_factor=-0.8;
  170. s->i_quant_offset=0.0;
  171. s->error_concealment= 3;
  172. s->error_resilience= 1;
  173. s->workaround_bugs= FF_BUG_AUTODETECT;
  174. s->frame_rate = 25 * FRAME_RATE_BASE;
  175. s->gop_size= 50;
  176. s->me_method= ME_EPZS;
  177. s->get_buffer= avcodec_default_get_buffer;
  178. s->release_buffer= avcodec_default_release_buffer;
  179. s->me_subpel_quality=8;
  180. }
  181. /**
  182. * allocates a AVCodecContext and set it to defaults.
  183. * this can be deallocated by simply calling free()
  184. */
  185. AVCodecContext *avcodec_alloc_context(void){
  186. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  187. if(avctx==NULL) return NULL;
  188. avcodec_get_context_defaults(avctx);
  189. return avctx;
  190. }
  191. /**
  192. * allocates a AVPFrame and set it to defaults.
  193. * this can be deallocated by simply calling free()
  194. */
  195. AVFrame *avcodec_alloc_frame(void){
  196. AVFrame *pic= av_mallocz(sizeof(AVFrame));
  197. return pic;
  198. }
  199. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  200. {
  201. int ret;
  202. avctx->codec = codec;
  203. avctx->codec_id = codec->id;
  204. avctx->frame_number = 0;
  205. if (codec->priv_data_size > 0) {
  206. avctx->priv_data = av_mallocz(codec->priv_data_size);
  207. if (!avctx->priv_data)
  208. return -ENOMEM;
  209. } else {
  210. avctx->priv_data = NULL;
  211. }
  212. ret = avctx->codec->init(avctx);
  213. if (ret < 0) {
  214. av_freep(&avctx->priv_data);
  215. return ret;
  216. }
  217. return 0;
  218. }
  219. int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  220. const short *samples)
  221. {
  222. int ret;
  223. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  224. avctx->frame_number++;
  225. return ret;
  226. }
  227. int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  228. const AVFrame *pict)
  229. {
  230. int ret;
  231. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  232. emms_c(); //needed to avoid a emms_c() call before every return;
  233. avctx->frame_number++;
  234. return ret;
  235. }
  236. /* decode a frame. return -1 if error, otherwise return the number of
  237. bytes used. If no frame could be decompressed, *got_picture_ptr is
  238. zero. Otherwise, it is non zero */
  239. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  240. int *got_picture_ptr,
  241. UINT8 *buf, int buf_size)
  242. {
  243. int ret;
  244. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  245. buf, buf_size);
  246. emms_c(); //needed to avoid a emms_c() call before every return;
  247. if (*got_picture_ptr)
  248. avctx->frame_number++;
  249. return ret;
  250. }
  251. /* decode an audio frame. return -1 if error, otherwise return the
  252. *number of bytes used. If no frame could be decompressed,
  253. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  254. *size in BYTES. */
  255. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  256. int *frame_size_ptr,
  257. UINT8 *buf, int buf_size)
  258. {
  259. int ret;
  260. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  261. buf, buf_size);
  262. avctx->frame_number++;
  263. return ret;
  264. }
  265. int avcodec_close(AVCodecContext *avctx)
  266. {
  267. if (avctx->codec->close)
  268. avctx->codec->close(avctx);
  269. av_freep(&avctx->priv_data);
  270. avctx->codec = NULL;
  271. return 0;
  272. }
  273. AVCodec *avcodec_find_encoder(enum CodecID id)
  274. {
  275. AVCodec *p;
  276. p = first_avcodec;
  277. while (p) {
  278. if (p->encode != NULL && p->id == id)
  279. return p;
  280. p = p->next;
  281. }
  282. return NULL;
  283. }
  284. AVCodec *avcodec_find_encoder_by_name(const char *name)
  285. {
  286. AVCodec *p;
  287. p = first_avcodec;
  288. while (p) {
  289. if (p->encode != NULL && strcmp(name,p->name) == 0)
  290. return p;
  291. p = p->next;
  292. }
  293. return NULL;
  294. }
  295. AVCodec *avcodec_find_decoder(enum CodecID id)
  296. {
  297. AVCodec *p;
  298. p = first_avcodec;
  299. while (p) {
  300. if (p->decode != NULL && p->id == id)
  301. return p;
  302. p = p->next;
  303. }
  304. return NULL;
  305. }
  306. AVCodec *avcodec_find_decoder_by_name(const char *name)
  307. {
  308. AVCodec *p;
  309. p = first_avcodec;
  310. while (p) {
  311. if (p->decode != NULL && strcmp(name,p->name) == 0)
  312. return p;
  313. p = p->next;
  314. }
  315. return NULL;
  316. }
  317. AVCodec *avcodec_find(enum CodecID id)
  318. {
  319. AVCodec *p;
  320. p = first_avcodec;
  321. while (p) {
  322. if (p->id == id)
  323. return p;
  324. p = p->next;
  325. }
  326. return NULL;
  327. }
  328. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  329. {
  330. const char *codec_name;
  331. AVCodec *p;
  332. char buf1[32];
  333. char channels_str[100];
  334. int bitrate;
  335. if (encode)
  336. p = avcodec_find_encoder(enc->codec_id);
  337. else
  338. p = avcodec_find_decoder(enc->codec_id);
  339. if (p) {
  340. codec_name = p->name;
  341. } else if (enc->codec_name[0] != '\0') {
  342. codec_name = enc->codec_name;
  343. } else {
  344. /* output avi tags */
  345. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  346. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  347. enc->codec_tag & 0xff,
  348. (enc->codec_tag >> 8) & 0xff,
  349. (enc->codec_tag >> 16) & 0xff,
  350. (enc->codec_tag >> 24) & 0xff);
  351. } else {
  352. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  353. }
  354. codec_name = buf1;
  355. }
  356. switch(enc->codec_type) {
  357. case CODEC_TYPE_VIDEO:
  358. snprintf(buf, buf_size,
  359. "Video: %s%s",
  360. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  361. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  362. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  363. ", %s",
  364. avcodec_get_pix_fmt_name(enc->pix_fmt));
  365. }
  366. if (enc->width) {
  367. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  368. ", %dx%d, %0.2f fps",
  369. enc->width, enc->height,
  370. (float)enc->frame_rate / FRAME_RATE_BASE);
  371. }
  372. if (encode) {
  373. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  374. ", q=%d-%d", enc->qmin, enc->qmax);
  375. }
  376. bitrate = enc->bit_rate;
  377. break;
  378. case CODEC_TYPE_AUDIO:
  379. snprintf(buf, buf_size,
  380. "Audio: %s",
  381. codec_name);
  382. switch (enc->channels) {
  383. case 1:
  384. strcpy(channels_str, "mono");
  385. break;
  386. case 2:
  387. strcpy(channels_str, "stereo");
  388. break;
  389. case 6:
  390. strcpy(channels_str, "5:1");
  391. break;
  392. default:
  393. sprintf(channels_str, "%d channels", enc->channels);
  394. break;
  395. }
  396. if (enc->sample_rate) {
  397. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  398. ", %d Hz, %s",
  399. enc->sample_rate,
  400. channels_str);
  401. }
  402. /* for PCM codecs, compute bitrate directly */
  403. switch(enc->codec_id) {
  404. case CODEC_ID_PCM_S16LE:
  405. case CODEC_ID_PCM_S16BE:
  406. case CODEC_ID_PCM_U16LE:
  407. case CODEC_ID_PCM_U16BE:
  408. bitrate = enc->sample_rate * enc->channels * 16;
  409. break;
  410. case CODEC_ID_PCM_S8:
  411. case CODEC_ID_PCM_U8:
  412. case CODEC_ID_PCM_ALAW:
  413. case CODEC_ID_PCM_MULAW:
  414. bitrate = enc->sample_rate * enc->channels * 8;
  415. break;
  416. default:
  417. bitrate = enc->bit_rate;
  418. break;
  419. }
  420. break;
  421. default:
  422. av_abort();
  423. }
  424. if (encode) {
  425. if (enc->flags & CODEC_FLAG_PASS1)
  426. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  427. ", pass 1");
  428. if (enc->flags & CODEC_FLAG_PASS2)
  429. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  430. ", pass 2");
  431. }
  432. if (bitrate != 0) {
  433. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  434. ", %d kb/s", bitrate / 1000);
  435. }
  436. }
  437. unsigned avcodec_version( void )
  438. {
  439. return LIBAVCODEC_VERSION_INT;
  440. }
  441. unsigned avcodec_build( void )
  442. {
  443. return LIBAVCODEC_BUILD;
  444. }
  445. /* must be called before any other functions */
  446. void avcodec_init(void)
  447. {
  448. static int inited = 0;
  449. if (inited != 0)
  450. return;
  451. inited = 1;
  452. //dsputil_init();
  453. }
  454. /* this can be called after seeking and before trying to decode the next keyframe */
  455. void avcodec_flush_buffers(AVCodecContext *avctx)
  456. {
  457. int i;
  458. MpegEncContext *s = avctx->priv_data;
  459. switch(avctx->codec_id){
  460. case CODEC_ID_MPEG1VIDEO:
  461. case CODEC_ID_H263:
  462. case CODEC_ID_RV10:
  463. case CODEC_ID_MJPEG:
  464. case CODEC_ID_MJPEGB:
  465. case CODEC_ID_MPEG4:
  466. case CODEC_ID_MSMPEG4V1:
  467. case CODEC_ID_MSMPEG4V2:
  468. case CODEC_ID_MSMPEG4V3:
  469. case CODEC_ID_WMV1:
  470. case CODEC_ID_WMV2:
  471. case CODEC_ID_H263P:
  472. case CODEC_ID_H263I:
  473. case CODEC_ID_SVQ1:
  474. for(i=0; i<MAX_PICTURE_COUNT; i++){
  475. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  476. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  477. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  478. }
  479. s->last_picture.data[0] = s->next_picture.data[0] = NULL;
  480. break;
  481. default:
  482. //FIXME
  483. break;
  484. }
  485. }
  486. static int raw_encode_init(AVCodecContext *s)
  487. {
  488. return 0;
  489. }
  490. static int raw_decode_frame(AVCodecContext *avctx,
  491. void *data, int *data_size,
  492. UINT8 *buf, int buf_size)
  493. {
  494. return -1;
  495. }
  496. static int raw_encode_frame(AVCodecContext *avctx,
  497. unsigned char *frame, int buf_size, void *data)
  498. {
  499. return -1;
  500. }
  501. AVCodec rawvideo_codec = {
  502. "rawvideo",
  503. CODEC_TYPE_VIDEO,
  504. CODEC_ID_RAWVIDEO,
  505. 0,
  506. raw_encode_init,
  507. raw_encode_frame,
  508. NULL,
  509. raw_decode_frame,
  510. };