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.

416 lines
13KB

  1. /*
  2. * YUV4MPEG format
  3. * Copyright (c) 2001, 2002, 2003 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. #define Y4M_MAGIC "YUV4MPEG2"
  21. #define Y4M_FRAME_MAGIC "FRAME"
  22. #define Y4M_LINE_MAX 256
  23. struct frame_attributes {
  24. int interlaced_frame;
  25. int top_field_first;
  26. };
  27. #ifdef CONFIG_MUXERS
  28. static int yuv4_generate_header(AVFormatContext *s, char* buf)
  29. {
  30. AVStream *st;
  31. int width, height;
  32. int raten, rated, aspectn, aspectd, n;
  33. char inter;
  34. char *colorspace = "";
  35. st = s->streams[0];
  36. width = st->codec->width;
  37. height = st->codec->height;
  38. av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
  39. aspectn = st->codec->sample_aspect_ratio.num;
  40. aspectd = st->codec->sample_aspect_ratio.den;
  41. if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
  42. inter = 'p'; /* progressive is the default */
  43. if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
  44. inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
  45. }
  46. switch(st->codec->pix_fmt) {
  47. case PIX_FMT_GRAY8:
  48. colorspace = " Cmono";
  49. break;
  50. case PIX_FMT_YUV411P:
  51. colorspace = " C411 XYSCSS=411";
  52. break;
  53. case PIX_FMT_YUV420P:
  54. colorspace = (st->codec->codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2";
  55. break;
  56. case PIX_FMT_YUV422P:
  57. colorspace = " C422 XYSCSS=422";
  58. break;
  59. case PIX_FMT_YUV444P:
  60. colorspace = " C444 XYSCSS=444";
  61. break;
  62. }
  63. /* construct stream header, if this is the first frame */
  64. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  65. Y4M_MAGIC,
  66. width,
  67. height,
  68. raten, rated,
  69. inter,
  70. aspectn, aspectd,
  71. colorspace);
  72. return n;
  73. }
  74. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  75. {
  76. AVStream *st = s->streams[pkt->stream_index];
  77. ByteIOContext *pb = &s->pb;
  78. AVPicture *picture;
  79. int* first_pkt = s->priv_data;
  80. int width, height, h_chroma_shift, v_chroma_shift;
  81. int i, m;
  82. char buf2[Y4M_LINE_MAX+1];
  83. char buf1[20];
  84. uint8_t *ptr, *ptr1, *ptr2;
  85. picture = (AVPicture *)pkt->data;
  86. /* for the first packet we have to output the header as well */
  87. if (*first_pkt) {
  88. *first_pkt = 0;
  89. if (yuv4_generate_header(s, buf2) < 0) {
  90. av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
  91. return AVERROR_IO;
  92. } else {
  93. put_buffer(pb, buf2, strlen(buf2));
  94. }
  95. }
  96. /* construct frame header */
  97. m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  98. put_buffer(pb, buf1, strlen(buf1));
  99. width = st->codec->width;
  100. height = st->codec->height;
  101. ptr = picture->data[0];
  102. for(i=0;i<height;i++) {
  103. put_buffer(pb, ptr, width);
  104. ptr += picture->linesize[0];
  105. }
  106. if (st->codec->pix_fmt != PIX_FMT_GRAY8){
  107. // Adjust for smaller Cb and Cr planes
  108. avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  109. width >>= h_chroma_shift;
  110. height >>= v_chroma_shift;
  111. ptr1 = picture->data[1];
  112. ptr2 = picture->data[2];
  113. for(i=0;i<height;i++) { /* Cb */
  114. put_buffer(pb, ptr1, width);
  115. ptr1 += picture->linesize[1];
  116. }
  117. for(i=0;i<height;i++) { /* Cr */
  118. put_buffer(pb, ptr2, width);
  119. ptr2 += picture->linesize[2];
  120. }
  121. }
  122. put_flush_packet(pb);
  123. return 0;
  124. }
  125. static int yuv4_write_header(AVFormatContext *s)
  126. {
  127. int* first_pkt = s->priv_data;
  128. if (s->nb_streams != 1)
  129. return AVERROR_IO;
  130. if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
  131. av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
  132. }
  133. else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
  134. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
  135. (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
  136. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
  137. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, yuv422p, yuv420p, yuv411p and gray pixel formats. Use -pix_fmt to select one.\n");
  138. return AVERROR_IO;
  139. }
  140. *first_pkt = 1;
  141. return 0;
  142. }
  143. static int yuv4_write_trailer(AVFormatContext *s)
  144. {
  145. return 0;
  146. }
  147. AVOutputFormat yuv4mpegpipe_oformat = {
  148. "yuv4mpegpipe",
  149. "YUV4MPEG pipe format",
  150. "",
  151. "y4m",
  152. sizeof(int),
  153. CODEC_ID_NONE,
  154. CODEC_ID_RAWVIDEO,
  155. yuv4_write_header,
  156. yuv4_write_packet,
  157. yuv4_write_trailer,
  158. .flags = AVFMT_RAWPICTURE,
  159. };
  160. #endif //CONFIG_MUXERS
  161. /* Header size increased to allow room for optional flags */
  162. #define MAX_YUV4_HEADER 80
  163. #define MAX_FRAME_HEADER 80
  164. static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
  165. {
  166. char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
  167. char *tokstart,*tokend,*header_end;
  168. int i;
  169. ByteIOContext *pb = &s->pb;
  170. int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0;
  171. enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
  172. AVStream *st;
  173. struct frame_attributes *s1 = s->priv_data;
  174. for (i=0; i<MAX_YUV4_HEADER; i++) {
  175. header[i] = get_byte(pb);
  176. if (header[i] == '\n') {
  177. header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
  178. header[i+2] = 0;
  179. break;
  180. }
  181. }
  182. if (i == MAX_YUV4_HEADER) return -1;
  183. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
  184. s1->interlaced_frame = 0;
  185. s1->top_field_first = 0;
  186. header_end = &header[i+1]; // Include space
  187. for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
  188. if (*tokstart==0x20) continue;
  189. switch (*tokstart++) {
  190. case 'W': // Width. Required.
  191. width = strtol(tokstart, &tokend, 10);
  192. tokstart=tokend;
  193. break;
  194. case 'H': // Height. Required.
  195. height = strtol(tokstart, &tokend, 10);
  196. tokstart=tokend;
  197. break;
  198. case 'C': // Color space
  199. if (strncmp("420jpeg",tokstart,7)==0)
  200. pix_fmt = PIX_FMT_YUV420P;
  201. else if (strncmp("420mpeg2",tokstart,8)==0)
  202. pix_fmt = PIX_FMT_YUV420P;
  203. else if (strncmp("420paldv", tokstart, 8)==0)
  204. pix_fmt = PIX_FMT_YUV420P;
  205. else if (strncmp("411", tokstart, 3)==0)
  206. pix_fmt = PIX_FMT_YUV411P;
  207. else if (strncmp("422", tokstart, 3)==0)
  208. pix_fmt = PIX_FMT_YUV422P;
  209. else if (strncmp("444alpha", tokstart, 8)==0) {
  210. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
  211. return -1;
  212. } else if (strncmp("444", tokstart, 3)==0)
  213. pix_fmt = PIX_FMT_YUV444P;
  214. else if (strncmp("mono",tokstart, 4)==0) {
  215. pix_fmt = PIX_FMT_GRAY8;
  216. } else {
  217. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
  218. return -1;
  219. }
  220. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  221. break;
  222. case 'I': // Interlace type
  223. switch (*tokstart++){
  224. case '?':
  225. break;
  226. case 'p':
  227. s1->interlaced_frame=0;
  228. break;
  229. case 't':
  230. s1->interlaced_frame=1;
  231. s1->top_field_first=1;
  232. break;
  233. case 'b':
  234. s1->interlaced_frame=1;
  235. s1->top_field_first=0;
  236. break;
  237. case 'm':
  238. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
  239. return -1;
  240. default:
  241. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  242. return -1;
  243. }
  244. break;
  245. case 'F': // Frame rate
  246. sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
  247. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  248. break;
  249. case 'A': // Pixel aspect
  250. sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
  251. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  252. break;
  253. case 'X': // Vendor extensions
  254. if (strncmp("YSCSS=",tokstart,6)==0) {
  255. // Older nonstandard pixel format representation
  256. tokstart+=6;
  257. if (strncmp("420JPEG",tokstart,7)==0)
  258. alt_pix_fmt=PIX_FMT_YUV420P;
  259. else if (strncmp("420MPEG2",tokstart,8)==0)
  260. alt_pix_fmt=PIX_FMT_YUV420P;
  261. else if (strncmp("420PALDV",tokstart,8)==0)
  262. alt_pix_fmt=PIX_FMT_YUV420P;
  263. else if (strncmp("411",tokstart,3)==0)
  264. alt_pix_fmt=PIX_FMT_YUV411P;
  265. else if (strncmp("422",tokstart,3)==0)
  266. alt_pix_fmt=PIX_FMT_YUV422P;
  267. else if (strncmp("444",tokstart,3)==0)
  268. alt_pix_fmt=PIX_FMT_YUV444P;
  269. }
  270. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  271. break;
  272. }
  273. }
  274. if ((width == -1) || (height == -1)) {
  275. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  276. return -1;
  277. }
  278. if (pix_fmt == PIX_FMT_NONE) {
  279. if (alt_pix_fmt == PIX_FMT_NONE)
  280. pix_fmt = PIX_FMT_YUV420P;
  281. else
  282. pix_fmt = alt_pix_fmt;
  283. }
  284. if (raten == 0 && rated == 0) {
  285. // Frame rate unknown
  286. raten = 25;
  287. rated = 1;
  288. }
  289. if (aspectn == 0 && aspectd == 0) {
  290. // Pixel aspect unknown
  291. aspectd = 1;
  292. }
  293. st = av_new_stream(s, 0);
  294. st = s->streams[0];
  295. st->codec->width = width;
  296. st->codec->height = height;
  297. av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
  298. av_set_pts_info(st, 64, rated, raten);
  299. st->codec->pix_fmt = pix_fmt;
  300. st->codec->codec_type = CODEC_TYPE_VIDEO;
  301. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  302. st->codec->sample_aspect_ratio= (AVRational){aspectn, aspectd};
  303. return 0;
  304. }
  305. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  306. {
  307. int i;
  308. char header[MAX_FRAME_HEADER+1];
  309. int packet_size, width, height;
  310. AVStream *st = s->streams[0];
  311. struct frame_attributes *s1 = s->priv_data;
  312. for (i=0; i<MAX_FRAME_HEADER; i++) {
  313. header[i] = get_byte(&s->pb);
  314. if (header[i] == '\n') {
  315. header[i+1] = 0;
  316. break;
  317. }
  318. }
  319. if (i == MAX_FRAME_HEADER) return -1;
  320. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
  321. width = st->codec->width;
  322. height = st->codec->height;
  323. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  324. if (packet_size < 0)
  325. return -1;
  326. if (av_get_packet(&s->pb, pkt, packet_size) != packet_size)
  327. return AVERROR_IO;
  328. if (s->streams[0]->codec->coded_frame) {
  329. s->streams[0]->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
  330. s->streams[0]->codec->coded_frame->top_field_first = s1->top_field_first;
  331. }
  332. pkt->stream_index = 0;
  333. return 0;
  334. }
  335. static int yuv4_read_close(AVFormatContext *s)
  336. {
  337. return 0;
  338. }
  339. static int yuv4_probe(AVProbeData *pd)
  340. {
  341. /* check file header */
  342. if (pd->buf_size <= sizeof(Y4M_MAGIC))
  343. return 0;
  344. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
  345. return AVPROBE_SCORE_MAX;
  346. else
  347. return 0;
  348. }
  349. AVInputFormat yuv4mpegpipe_iformat = {
  350. "yuv4mpegpipe",
  351. "YUV4MPEG pipe format",
  352. sizeof(struct frame_attributes),
  353. yuv4_probe,
  354. yuv4_read_header,
  355. yuv4_read_packet,
  356. yuv4_read_close,
  357. .extensions = "y4m"
  358. };
  359. int yuv4mpeg_init(void)
  360. {
  361. av_register_input_format(&yuv4mpegpipe_iformat);
  362. #ifdef CONFIG_MUXERS
  363. av_register_output_format(&yuv4mpegpipe_oformat);
  364. #endif //CONFIG_MUXERS
  365. return 0;
  366. }