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.

206 lines
5.1KB

  1. /*
  2. * Raw Video Codec
  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. /**
  20. * @file raw.c
  21. * Raw Video Codec
  22. */
  23. #include "avcodec.h"
  24. typedef struct PixleFormatTag {
  25. int pix_fmt;
  26. unsigned int fourcc;
  27. } PixelFormatTag;
  28. const PixelFormatTag pixelFormatTags[] = {
  29. { PIX_FMT_YUV422, MKTAG('Y', '4', '2', '2') },
  30. { -1, 0 },
  31. };
  32. static int findPixelFormat(unsigned int fourcc)
  33. {
  34. const PixelFormatTag * tags = pixelFormatTags;
  35. while (tags->pix_fmt >= 0) {
  36. if (tags->fourcc == fourcc)
  37. return tags->pix_fmt;
  38. tags++;
  39. }
  40. return PIX_FMT_YUV420P;
  41. }
  42. typedef struct RawVideoContext {
  43. unsigned char * buffer; /* block of memory for holding one frame */
  44. unsigned char * p; /* current position in buffer */
  45. int length; /* number of bytes in buffer */
  46. } RawVideoContext;
  47. static int raw_init(AVCodecContext *avctx)
  48. {
  49. RawVideoContext *context = avctx->priv_data;
  50. if (avctx->codec_tag) {
  51. avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
  52. }
  53. context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  54. context->buffer = av_malloc(context->length);
  55. context->p = context->buffer;
  56. if (! context->buffer) {
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. static int raw_decode(AVCodecContext *avctx,
  62. void *data, int *data_size,
  63. uint8_t *buf, int buf_size)
  64. {
  65. RawVideoContext *context = avctx->priv_data;
  66. int bytesNeeded;
  67. AVPicture * picture = (AVPicture *) data;
  68. /* Early out without copy if packet size == frame size */
  69. if (buf_size == context->length && context->p == context->buffer) {
  70. avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
  71. *data_size = sizeof(AVPicture);
  72. return buf_size;
  73. }
  74. bytesNeeded = context->length - (context->p - context->buffer);
  75. if (buf_size < bytesNeeded) {
  76. memcpy(context->p, buf, buf_size);
  77. context->p += buf_size;
  78. *data_size = 0;
  79. return buf_size;
  80. }
  81. memcpy(context->p, buf, bytesNeeded);
  82. context->p = context->buffer;
  83. avpicture_fill(picture, context->buffer, avctx->pix_fmt, avctx->width, avctx->height);
  84. *data_size = sizeof(AVPicture);
  85. return bytesNeeded;
  86. }
  87. static int raw_close(AVCodecContext *avctx)
  88. {
  89. RawVideoContext *context = avctx->priv_data;
  90. av_freep(& context->buffer);
  91. return 0;
  92. }
  93. static int raw_encode(AVCodecContext *avctx,
  94. unsigned char *frame, int buf_size, void *data)
  95. {
  96. AVPicture * picture = data;
  97. unsigned char *src;
  98. unsigned char *dest = frame;
  99. int i, j;
  100. int w = avctx->width;
  101. int h = avctx->height;
  102. int size = avpicture_get_size(avctx->pix_fmt, w, h);
  103. if (size > buf_size) {
  104. return -1;
  105. }
  106. switch(avctx->pix_fmt) {
  107. case PIX_FMT_YUV420P:
  108. for(i=0;i<3;i++) {
  109. if (i == 1) {
  110. w >>= 1;
  111. h >>= 1;
  112. }
  113. src = picture->data[i];
  114. for(j=0;j<h;j++) {
  115. memcpy(dest, src, w);
  116. dest += w;
  117. src += picture->linesize[i];
  118. }
  119. }
  120. break;
  121. case PIX_FMT_YUV422P:
  122. for(i=0;i<3;i++) {
  123. if (i == 1) {
  124. w >>= 1;
  125. }
  126. src = picture->data[i];
  127. for(j=0;j<h;j++) {
  128. memcpy(dest, src, w);
  129. dest += w;
  130. src += picture->linesize[i];
  131. }
  132. }
  133. break;
  134. case PIX_FMT_YUV444P:
  135. for(i=0;i<3;i++) {
  136. src = picture->data[i];
  137. for(j=0;j<h;j++) {
  138. memcpy(dest, src, w);
  139. dest += w;
  140. src += picture->linesize[i];
  141. }
  142. }
  143. break;
  144. case PIX_FMT_YUV422:
  145. src = picture->data[0];
  146. for(j=0;j<h;j++) {
  147. memcpy(dest, src, w * 2);
  148. dest += w * 2;
  149. src += picture->linesize[0];
  150. }
  151. break;
  152. case PIX_FMT_RGB24:
  153. case PIX_FMT_BGR24:
  154. src = picture->data[0];
  155. for(j=0;j<h;j++) {
  156. memcpy(dest, src, w * 3);
  157. dest += w * 3;
  158. src += picture->linesize[0];
  159. }
  160. break;
  161. default:
  162. return -1;
  163. }
  164. return size;
  165. }
  166. AVCodec rawvideo_codec = {
  167. "rawvideo",
  168. CODEC_TYPE_VIDEO,
  169. CODEC_ID_RAWVIDEO,
  170. sizeof(RawVideoContext),
  171. raw_init,
  172. raw_encode,
  173. raw_close,
  174. raw_decode,
  175. };