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.

212 lines
5.3KB

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